Matlab tutorial 1a: crash-course for noobs

I started using Matlab just over 1 year ago. The most important thing I’ve learned over that 1-year period is that Matlab is, hands down, the best scientific programming tool I’ve ever used. Matlab code is easy to write. The front-end application (GUI/IDE) is rock solid. You can surround yourself with help panes, file navs, code tools, debugging interfaces, etc., or not (if you like a minimalist coding environment) – from code coloring to collapsing, everything about the layout is customizable (Fig1)

Fig1: Clockwise from 12 o'clock (the arrow pointing at 'MATLAB'). (1) the arrow pointing at 'MATLAB' (2) Shortcuts (3) code view panes (4) Help (5) additional editor pane (6) variable workspace (7) Start bar (8) command window (9) Current Folder (10) currently open scripts (11) GUIDE (12) Debug (13) run.
Fig1: Here’s my custom Matlab IDE layout. Clockwise from 12 o’clock (the arrow pointing at ‘MATLAB’). (1) the arrow pointing at ‘MATLAB’ (2) Shortcuts (3) code view panes (4) Help (5) additional editor pane (6) variable workspace (7) Start bar (8) command window (9) Current Folder (10) currently open scripts (11) GUIDE (12) Debug (13) run.

I highly suggest spending a few minutes customizing your Matlab IDE. Once you get everything laid out the way you like, you can save this custom layout for next time. I’m not going to tell you how you’re layout should be set-up, but I would highly recommend having a help glider on the right and a file nav glider on the left (these will be used often). It’s also nice to have both the command window and variable workspace below the editor panes to maximize left-to-right viewing-space for the code editor panes (as your scripts get longer you’ll be more likely to split the window than scrolling vertically between POIs). Your customized Matlab environment will be an evolving art for the first few months, so don’t worry about getting everything nip-tuck on your first launch.

Once you’ve sufficiently tinkered with the layout features, the next, and most important thing to do is setting up your Matlab PATH. If you’re a programming novice do not skip this step! It will save you tons of headache down the road (when you’re wondering why updates to your functions aren’t being implemented). Whether you’re a coding novice or pro, you’ll be happy to know that setting up your Matlab PATH is easy. Here’s the steps I recommend for setting up the Matlab PATH:

  • 1. Create a folder in your current documents folder called “Matlab” (or “MatlabFiles”).
  • 2. In this new “Matlab” folder, create a “myMatlab”, “myArchive”, and “myToolbox” folder
  • 3. Also in the “Matlab” folder, create a folder for each new project (or homework assignment) you’re working on. I typically move these individual project folders into the “myMatlab” or “myArchive” depending on how likely I’ll be coming back to that code.
  • 4. If you already have a few general-purpose functions, organize those into subfolders in the “myToolbox” folder.
  • 5. Open Matlab and click on “File” then “Path” (see Fig2 below)
  • 6. Remove anything that wasn’t in the PATH by default (and obviously anything that you know you want to keep in the PATH)
  • 7. Click on “Add Folder…” and select ‘Matlab’, ‘myMatlab’, and ‘myToolbox’ (n.b. For this step do not select “Add with Subfolders…” – this will add everything inside the selected ‘Matlab’ folder including old functions located in your myArchive folder which can cause problems down the road).
  • 8. Click “Save” then “Close”.
  • 9. Pop open the file navigation glider (see Fig3 below); you’ll see that everything not added to Matlab’s PATH is greyed while everything in Matlab’s PATH is effulgent. This is exactly what you want.
Fig2. Setting Matlab's PATH.
Fig2. Setting Matlab’s PATH.
Fig3. Checking that the correct files were added to Matlab's PATH.
Fig3. Checking that the correct files were added to Matlab’s PATH.

The 10th step:
Now that your Matlab folders are nicely organized and have a basic PATH established, there is only one final step in setting your Matlab PATH. But in order to do this, you really should take a second to familiarize yourself with how Matlab “sees” files. In brief:

The search path, or path is a subset of all the folders in the file system. MATLAB® software uses the search path to efficiently locate files used with MathWorks® products. MATLAB can access all files in the folders on the search path. The order of folders on the search path is important. When files with the same name appear in multiple folders on the search path, MATLAB uses the one found in the folder nearest to the top of the search path.

So to summarize, the Matlab software can see all the scripts and functions saved in any folder in your PATH (and also the folder you are currently working in). If your scripts/functions make calls to other homebrew, shared, or filexchange functions, Matlab’s PATH will need to be updated to include these files. However, if you are no longer using a particular function, or have a newer version of that function, remember to remove the outdated versions to an archive folder not in Matlab’s PATH (otherwise Matlab may use the old function instead of the new function, and sometimes you won’t even noticed this happened).