A good course for MATLAB is Mike X Cohen’s course on Udemy.

Course notes

GUI

  • Can set more information for variables. Recommended information to add apart from variable name and value is: size, bytes, type.

Variables

  • Type character array and string are different. Basically best not to use string type. Not sure what it is used for.

    s1 = 'char_array';
    s2 = "string";
    s1(2) % gives 'h'
    s2(2) % gives an error
  • Reminder: Square (array) allows concatenation. Cell arrays on the other hand will create cells for every value between the curly brackets

    ['Hello' 'World']
    [4:9 10:11 pi]
    % above from different
    { 4 5 6}
    {'Hello' 'World'}
  • Multiline comments (to use if needed)

    %{
    Can write anything here as many lines as I want!
    }%
  • Structures
    Good use of structures to replace “objects”. The nice part of this is that you can make struct arrays with particular types of elements.

    struct(1).name = "Tom"
    struct(1).age = 30
     
    struct(2).name = "Ron"
    struct(2).age = 45

Functions

Mike talks about a nice way of writing functions. Starting with comments to describe the algorithm the filling in the details. This is a nice way of forcing writing down the algorithm before coding (which often is the cause of long/confusing code).

Read and writing

  • Can read ‘clean’ text files using load. Also can use the standard, xlsread (or csvread), or just load for matlab files

    load('data.txt')
  • dlmwrite allows to write data with any defined delimiters \t for tab or space or comma. csvwrite is a specific instance of this.

Plotting

  • imagesec - good for visualizing matrices

Debugging

  • Use 1i instead of i (will get confused with for i iterator)
  • .* works as element to element multiplication only if the matrix are of the same sizes (row, col numbers). If the row columns are flipped, matlab will silently do a outer product multiplication.
  • tic-toc
    • can use to measure how long a few lines of code take to run.
  • matlab profiler
    • a nice way of measuring how much different lines of code take
    • seems super useful actually
    • not sure why I was not using it 😅
    • there seems to be some tmpscript issue - that is code needs to be moved to a file called tmpscript. Not sure if this has to be exactly the specific one or just any script file.

Tips

  • From 2019/2020, there is an addon manager which one can use to install toolboxes you want, so no need for a 30GB all toolbox install anymore! You will have to sign in with your account though, but the storage save is massive.

  • Archlinux + sway
    For sway, you need to enable _JAVA_AWT_WM_NONREPARENTING=1. Although you might have defined it in the environment, for some reason running matlab from commandline does not automatically use the variable. Create a desktop entry, which seems to work well, or use the following line:

    Exec=env _JAVA_AWT_WM_NONREPARENTING=1 matlab