What is the MATLAB tip that you would share with your younger self?
Hans Scharler
on 10 Jan 2024
I would tell myself to understand vectorization. MATLAB is designed for operating on whole arrays and matrices at once. This is often more efficient than using loops.
26 Comments
Use live scripts to tell your computational story. What Is a Live Script or Function? - MATLAB & Simulink - MathWorks United Kingdom
Combined with interactive 'Live controls' Add Interactive Controls to a Live Script - MATLAB & Simulink - MathWorks United Kingdom and 'Live tasks' Add Interactive Tasks to a Live Script - MATLAB & Simulink - MathWorks United Kingdom you can use these to communicate your ideas extremely well.
Finally, since you took my advice on version control, you can go an extra step and make your interactive computational story available to anyone in the world -- whether not not they are a MATLAB user.
I'd probably say: Don't assume you know where the slow part of your code is. Use the profiler! While you do pick up some intuition over time, nothing beats getting profiler results to prove where your time is best spent on high-intensity code. (And: don't waste time trying to optimize something before you have things working as intended!)
I'd also strongly second Mikes suggestion of Version control (and especially git). Although my main points are how much branching between team members can help track where things are happening. Edit to add: especially with PROJECTS! (although they didnt exist when I got started so maybe unfair). Managing path changes, keeping all resources together, simple export to a project archive to share and KNOW that the path will be setup correctly on someone elses machine... very nice to have that all happen in the background and not be something I have to deal with every time I decide to share some code.
Use test-driven development (TDD)! In my estimation, you will gain confidence in your own programming skills fastest by coding based on tests and ensuring that every aspect is tested. Then, if something goes wrong or additional features need to be added, you can proceed knowing that once all the tests pass (again) your fixes/improvements/additions/etc. did not break existing functionality. And, that the fixes and/or additions also work as expected.
Use version control and sync with an online repository such as GitHub or Bitbucket.
Even if your project is just one .m file, you are the only developer and you think that it's not worth using version control because your code is only 10 lines...do it, do it now.
Even if you don't understand why you should use verson control and you disagree with me for whatever reason...shhhh, just make an old man happy and start using git. The online repo can even be private if you want. I won't judge
Just...please...look at the scars on my face, the grey hairs and the look in my eyes that tell you I've been there, I've seen and experienced things that you are too nice to see and experience. You don't want these memories, this pain. Be kind to yourself, use version control.
Once you get used to the basics: an add, a commit, a push...maybe you'll start learning about some of the other benefits of taking this path..or maybe not. It doesn't matter, you took your first step into a larger world...and I'm happy for you
One day, long from now, I'll be in Florence. There's this cafe on the banks of the Arno. Every fine evening, I'll sit there and order a Fernet Branca. I have this fantasy, that I would look across the tables and I'd see you there, with a partner and maybe a couple of kids. You wouldn't say anything to me, nor me to you. But we'd both know that you'd made it, that you were happy.....
because today you started using version control.
I would have made myself aware of the copy-on-write memory management system that Matlab uses. The early Matlab documentation was misleading (and could still use some improvement IMHO) in that it said that there was no passing by reference in Matlab, but rather that all argument passing was by value. It wasn't true - it was just a way of telling new Matlab users in an over-simplified way that they don't have to worry about learning pointers and references like in C/C++.
This led to a lot of unnecessary anxiety and a lot of awkward acrobatic coding on my part as I tried to limit memory consumption. A rewrote a ton once I learned that passing by reference was a thing in Matlab, just automated and hidden from me by copy-on-write.
Learn to do things right straight away
Disclaimer: I am a trainer at MathWorks, so I am admittedly biased. :)
Even though I joined MathWorks in 2007, I still remember taking MATLAB Fundamentals and being amazed how much I didn't know having used MATLAB for over 6 years prior to joining MathWorks, and I didn't even want to think about how much time knowing these things would have saved me.
Customers have told me during courses that they wish they had taken a course months or even years ago, and thus had saved a lot of time and effort.
If you are new to an area or keep thinking that there must be an easier or better way of doing what you are doing, check with your favorite colleague at MathWorks or explore training offerings yourself:
- instructor-led
- self-paced (some of those are free!)
If you are not sure which offering is best for you, please do not hesitate to check with me.
Add commonly used commands to your favorites list and add your top favorites to the quick access toolbar. A real time-saver!
Leverage technical support:
I faced crashes during my master thesis project and it was likely simply caused by a graphic driver crash. If I had known how to start MATLAB with -softwareopengl flag I would have saved some struggles.
Here you can find ways on how to contact technical support:
and
You can also send crash logs to us, if you enter a valid mail address in the crash reporter. If you reply to the mail you get from our system if you still need help, we will support you!
Just get it working. If it's really that important you're probably going to rewrite the whole thing over the summer anyway. 😊
Why I would tell my younger self: early on I spent too much time on unimportant details for the sake of completeness and it slowed me down. Later it became clearer which analyses were important and I ended up restructuring the whole pipeline anyway.