MATLAB Community

MATLAB, community & more

Organizing your files for fast coding

Pascal Getreuer over at the widget-heavy Advanced MATLAB blog recently wrote several posts about making your code run faster, the first of which described file organization. Pascal’s suggestions will help your code run faster, but there are number of features built into the MATLAB Desktop to help you write your code faster.

In that article, he makes the following suggestions for organizing MATLAB code as “good working habits.”

  • Use a separate folder for each project
  • Write header comments, especially H1
  • Save frequent console commands as a script

These are great ideas, and here some parts of the Desktop that facilitate those habits:

Managing Folders
The Current Folder Browser provides a few features to make managing separate folders easier.

The tree view lets you look at multiple projects from a root directory. Having a top-down hierarchical view lets you easily inventory and move files between project directories.

Current Folder view of Project tree

The address bar lets you quickly switch back and forth between project directories. This lets you keep only one of these folders on the MATLAB search path at the same time.

Current Folder address bar

If you have a nested folder structure of useful functions that you want to access (for example a heirarchical tools directory), you can use the “Add with Subfolders” from the context menu to quickly add a whole directory tree to your MATLAB search path.

Add with subfolders on Current Folder browser context menu

Write Header Comments
Having H1 comment lines in your files enables your functions, scripts, and classes to participate in functions like help and lookfor. When you supply a directory to the help function it reads out a list of functions in that directory. You can customize this display with a Contents.m file, which you can generate with the Contents Report. I described in earlier post how to create a Contents.m with the report.

Generate Contents.m with contents report

Save Console commands as a script
You can select several commands in the Command History and create a script out it. Check out our earlier post on creating a file from the History. The method I use is: (1) delete from the Command History the unwanted commands so that the ones I’m interested in is left as a contiguous block. (2) Then I create a file in the Editor, (3) select in the History those commands and (4) drag the whole block into the Editor. Instant script!

You can also drag those commands to the shortcut bar to create a new shortcut instead.

  • print


To leave a comment, please click here to sign in to your MathWorks Account or create a new one.