MATLAB Answers

0

What are the thought processes that go on when you are trying to solve a MATLAB problem?

Asked by Manny Kins on 2 May 2019
Latest activity Edited by Stephen Cobeldick on 2 May 2019
I am (fairly) new to MATLAB, just started using it more heavily these past few months (before then I just used it to make basic plots). So when I have an issue I typically spend hours on just the (seemingly) simplest things such as how to remove certain values from structures and how to improve image quality for image analysis etc...
I just wanted to know if there is a particular thought process that you (everyone) go though before finding a solution to your exact problem. This might sound like a pretty dumb question but I think maybe learning about how other people tackle code related problems may be able to help me add good habits to what I do and make me improve.
Thanks

  2 Comments

@Manny Kins: for some MATLAB code-writing tips, you might like to read this:
There are plenty of methods, processes, and approaches to help write code, which I am sure that an internet search will help you with.
"I typically spend hours on just the (seemingly) simplest things such as how to remove certain values from structures..."
As long as you spend those hours reading the documentation and trying examples, then that is a normal and productive way to learn how to write code. No one (with the possible exception of Donald Knuth) was born knowing how to write code. It has to be learned. And that takes time and reading the documentation. A lot.
Personally I think that one of the hardest parts to code writing is learning the names of things: if you do not know what a thing is named then searching for its documentation or explanation can be very difficult. Reading as much as possible is one remedy, or by taking part some structured learning (e.g. online/MOOC courses).
Tip: do not underestimate the data design! Good data organisation leads to much better code. A clear and logical organisation of data will make the rest of the code exponentially simpler and more efficient. As a general rule of thumb, leave data together as much as possible.
Hi Stephen, thank you it is very reassuring knowing that spending a lot of time learning and progressing. I guess it can be frustrating trying to solve something when you don't have the right "toolkit" in your repertoire so learning and trying to implement the documentation will do me a lot of good. The link had extremely valuable information which I will try and take on board, thanks for your time.

Sign in to comment.

1 Answer

Answer by Adam
on 2 May 2019
Edited by Adam
on 2 May 2019
 Accepted Answer

I make very heavy use of the debugger, in combination with the command line to try out syntax and options. I also often like to create quick scripts to test out alternative solutions to a problem where speed is an issue and I want to understand which is fastest.
I make heavy use of the offline documentation, using e.g.
doc ismember
or whatever function I want to use, looking at the 'See Also'; section or any other links from the page if the function itself is not what I want, but I feel I need something similar.
I keep a series of e-mails (effectively a blog, but for reasons not interesting here, e-mails were the chosen medium) in which I write up what I call 'Matlab Nuggets' which remind me (and my other team members, who I also send them to) about certain functions or how I solved a certain problem or reusable code I added to our repository. Basically just anything I think I or my team may find useful in future that i can search for.
Our team also has a fairly large repository of past code now so if I know I have solved a similar problem (or part of it) before I will do a search in our repository to find the relevant code if I can.
I also use other sources such as Matlab Answers and Yair Altman's Undoucmented Matlab blog, although this tends to be more hardcore! One of the main reasons I started answering questions on Matlab Answers is because it helps me learn. So many people post questions that can easily be answered just by testing out some syntax on command line - apparently they don't think of that, but me doing it and providing the answer they could just have easily found themselves helps various concepts or syntaxes sink into my brain too for future usage.

  2 Comments

Thank you for your insight Adam! The part where you mentioned to keep improving code that is slow really hits home. I currently have a code which analyses 18000 frames from a video and sometimes it takes over 20 hours to run! So I will certainly keep trying to improve that performance (maybe through vectorization which I heard is a big selling point of MATLAB and something I really need to learn to get to grips with).
I am also going to steal your idea with keeping documentation on past problems and how they were solved. I do not work for a company (just a PhD student getting by) but at least for myself it will be very useful to have something to refer to when I am stuck with a similar problem. Forgetting things is so easy!
Thanks again for taking your time to give me advice
"The part where you mentioned to keep improving code that is slow really hits home..."
This is also one of the hardest parts of being responsible for some code, because at some point you will have the choice: either improve/update that code and make it incompatible with existing data/functions (and make all of its users quite grumpy) or stick with some slower, buggier version (and make all of its users quite grumpy). There is no way to avoid this, it will happen:
The best solution is to use best-practice as much as you can (or is reasonable for your needs): keep track of code versions (best: with a version system), encapsulate functionality in functions or classes with clearly defined interfaces, keep data together as much as possible, etc..

Sign in to comment.