Tips & Tricks
Follow


Hans Scharler

What is the MATLAB tip that you would share with your younger self?

Hans Scharler on 10 Jan 2024
Latest activity Reply by Stephen23 on 20 Feb 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.
Mike Croucher
Mike Croucher on 12 Jan 2024
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.
Sam Reinsel
Sam Reinsel on 12 Jan 2024 (Edited on 12 Jan 2024)
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.
goc3
goc3 on 12 Jan 2024 (Edited on 12 Jan 2024)
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.
Mike Croucher
Mike Croucher on 12 Jan 2024
goc3
goc3 on 12 Jan 2024
Visit the Cody website often—it's a great place to practice using MATLAB and learn new ways to solve problems ranging from simple to complex.
Hans Scharler
Hans Scharler on 12 Jan 2024
What's your favorite Cody puzzle set for newcomers?
goc3
goc3 on 12 Jan 2024
There are well over a hundred Problem Groups, so it is hard to choose just one. I would pick out the following as good starting points, though:
Just for Fun: Draw numbers! and Draw Letters
Math challenge: Divisible by x
Indexing (the first in a Group series): Indexing I
Text operations (one of many such Groups): Strings I
There are also many Groups that have "basic" in their title.
Mike Croucher
Mike Croucher on 11 Jan 2024 (Edited on 11 Jan 2024)
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.
Harald
Harald on 12 Jan 2024
Yeah, I have come across a number of posts (not only, but also around MATLAB) by people trying to recover files or versions of files that got lost for whatever reasons. They would likely not have had this situation if they had used version control. Choose whether you want to learn from Mike, or learn the hard way yourself. ;)
Matt J
Matt J on 11 Jan 2024
Uggh. GitHub? Why? It only lets you log text files. What about .mat files and other binaries?
Hans Scharler
Hans Scharler on 12 Jan 2024

You can do mat and mlx on GitHub. It does not render them, but you can sync those formats using GitHub.

Mike Croucher
Mike Croucher on 11 Jan 2024
What would you suggest instead?
Stephen23
Stephen23 on 20 Feb 2024
Matt J
Matt J on 11 Jan 2024 (Edited on 12 Jan 2024)
The File Exchange. Google Drive. Duplicati.
I acknowledge these lack the full range of capabilities we expect from version control. However, I don't see an alternative for people whose repositories need to include non-text components.
Mike Croucher
Mike Croucher on 12 Jan 2024
As @Hans Scharler said, you can put such files under git version control. Sometimes it is a good idea, other times it isn't.
One problem with binary formats is that they don't work well with line based version control -- which git is. When you have a 10,000 line .m file and change one line, the only additional storage needed is essentially that one line change.
If you have a 10Mb binary file and change a tiny part of it, the entire binary needs to be replicated. The repo can get big quickly...but you can still do it. You can put any file you like on github!
In practice, however, it pays to think about things carefully. You have written a function that does some analysis...obviously the .m files go on GitHub..also a binary .mlx for demonstration purposes discussed above. The big .mat file though...where should that go? Maybe that's the only bit that goes elsewhere? Somewhere more suited for data storage. Does every user of your code need your data? Does it make sense to seperate code and data for your project?
Code needs a proper version control system and alll the benefits that come with it.
Matt J
Matt J on 11 Jan 2024 (Edited on 12 Jan 2024)
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.
Harald
Harald on 11 Jan 2024 (Edited on 11 Jan 2024)
Hi Matt,
if you don't mind me being curious: how did you end up learning about copy-on-write?
And... you are aware about the "next level", in-place assignments?
Cheers,
Harald
Matt J
Matt J on 11 Jan 2024
Hi Harald,
My research advisor made me aware of copy-on-write when I was a grad student, which led me to google more deeply into the subject and to Loren's Blog.
And yes, I am aware of in-place assignments.
Harald
Harald on 11 Jan 2024
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:
If you are not sure which offering is best for you, please do not hesitate to check with me.
Mark Cafaro
Mark Cafaro on 11 Jan 2024 (Edited on 11 Jan 2024)
Add commonly used commands to your favorites list and add your top favorites to the quick access toolbar. A real time-saver!
Harald
Harald on 11 Jan 2024
A nice one, for sure!
My favorite shortcut:
clear
close all
clc
Cheers,
Harald
Hans Scharler
Hans Scharler on 11 Jan 2024
This^
Oliver Jaehrig
Oliver Jaehrig on 11 Jan 2024
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!
Harald
Harald on 11 Jan 2024
Another way of contacting Technical Support: the "Request Support" button in the resources pane, in the very right of the Home tab... and surrounded by other useful resources. :)
Adam Danz
Adam Danz on 10 Jan 2024 (Edited on 11 Jan 2024)
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.
Hans Scharler
Hans Scharler on 11 Jan 2024

Great tip. Perfection is the enemy of good. Fail fast.

See Also