Skip to main content

Ch 4: Introduction to Matlab Programming

This chapter gives basic programming advice. It may be worth a deeper look if you are very new to programming, but if you have development experience honestly most of the advice is extremely obvious

4.1 Write Clean and Efficient Code

Organized code that is easier to read is easier to understand.

  • Use comments occasionally to explain blocks of code (commenting each line is excessive)
    • focus comments not on what you are literally doing, but instead give a justification for why you are doing it
  • Organized code runs faster (IE calculating an equation that always yields the same result in a loop is pointless)
  • Write code as an extensions of ones problem solving method. It should be clear what you are doing as you go from one step to the next
4.2 Use Meaningful File and Variable Names

Don' type gibberish are use the default "Untitledx.m" naming scheme for files, or for variables. Name everything appropriately such that the later you can find things easily (and so other people can navigate your code base easily).

(it is common code practice to use single letter variable names for iterators in loops. there is some contention at this, but this is the one general exception to variable naming rules).

4.3 Make Regular Backups of Your Code and Keep Original Copies of Modified Code

This chapter encourages manually saving multiple file copies of your code with every edit. I HEAVILY disagree with this approach. Instead, please learn to use Git, put in descriptive commit names, and use branches / merging instead. It is far easier to maintain and visualize progress and the steps taken to each step.
If you want a GUI client, I suggest SourceTree on Windows and Mac

4.4 Initialize Variables

Initializing variables in Matlab before usage (IE, initializing them outside of loops, defining the size of an array rather than appending to one repeatedly) is faster than not doing this. It can also contribute to better code organization

4.5 Help!

This section discusses common problems and possible solutions

  • Don't know how to use a specific function
    • type 'help FUNCTION_NAME' or in some cases 'doc FUNCITON_NAME' for a general explanation. A lot of functions can be opened by you to read its source code
  • You cannot figure out the command or function to do what you want to do
    • Google is your friend here, or ask a friend
  • There are errors in your code
    • Actually read the error
    • break up multistep single line statements into single line steps for easier debugging
    • Check for weird values in function inputs (NaNs, infinities)
    • Make sure any expected function is in Matlabs Path so it is aware it exists
4.6 Be Patient and Embrace the Learning Experience

Git Gud

Programming is a skill that takes time. Do not rely on tools like ChatGPT, spend the time learning the basics yourself and build your way up. It takes quite a bit of experience to become a decent programmer, so don't feel bad as a programmer

4.7 Excercises

This section is a list of exercises to complete in Matlab. You can find the answers for chapter 4 (and others) here.

My quick note for Python Programmers

Please, for the love of all that is holy, learn what virtual environments are and use them. Do NOT install to the system path, ESPECIALLY ON SHARED LAB COMPUTERS. If everyone follows this rule, you will be much less likely to step on each-others toes when using various dependencies

For newbies, I suggest either using the environment tools of Anaconda or the ones in, my preference, Pycharm Community Edition (scroll down for the free community edition, the professional one costs money for features you likely do not need)