Mike on the MATLAB Desktop
November 30th, 2009
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.
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.
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.
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.
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.
By
Michael Katz
Mike is a developer on the MATLAB Mobile team. When not describing himself in the third person, biking, homebrewing, or rooting for the home team, he's busy trying to make the world a better place for programming.
14:15 UTC |
Posted in Current Directory |
Permalink |
13 Comments »
You can follow any responses to this entry through the RSS 2.0 feed.
You can skip to the end and leave a response. Pinging is currently not allowed.
Leave a Reply
|
That reminds me that what I’d like is an option for the “add selected folder and subfolders to path” option to ignore hidden subfolders.
I use Subversion to version control my changes to my function libraries, and adding with subfolders adds all the “.svn” subfolders of the working copy. I don’t want to do an export of the files that doesn’t have these subfolders because it would not be in version control.
Is there perhaps a way other than writing a function that does it?
Ryan, no at this time the context menu function will grab all the subfolders.
I second the feature request Ryan made. We current had to write our own function which allows us to remove any “dots” folders- those starting with “.” like “.svn”. This also helps us on Linux platforms where the home directory has a lot of hidden dot folders. It would be nice if addpath() and genpath() took an optional argument for an exclude string.
Dan and Ryan, I will forward your suggestion to the path function developers. Thanks.
I have used this in the past to get a list of directories/files, etc.. ignoring .sn directories.
http://www.mathworks.com/matlabcentral/fileexchange/9647-grep-a-pedestrian-very-fast-grep-utility
More then what you are asking, but a great utility that will accomplish what you are asking for.
After you get the list of directories from the grep utility, use the addpath function to add them to the path.
Stephen
A better solution would be a simple project management support. I’m suggesting something similar to the visual studio project file. One main reason is that two different project may have the same file names and if you use the method described above without careful attention, you’ll end up adding all project paths in the path. Switching between project (e.g. changing current working directory and opening related files) does not automatically fixes path info. Also, you cannot open recently opened files in each project automatically.
Personally, I made my own project manager to control these things. But the best solution should be native support of the project file by Matlab itself.
@Sung,
That is a good point. I just described some easy steps that are currently available in MATLAB. If you have submitted your project manager to the File Exchange, please share the link here.
I would love Matlab to include more project management support. We have a terrible time keeping track of code associated with various projects.
KE,
It is something also that we are working on, but right now the easy way to use separate folders for each project, with a setup script that handles adding various folders to the path. It’s not the prettiest workflow, I admit.
I need a project management software for matlab.
whenever i make some changes in the code, that software should make backup. Or else atleast when i open a file and edit them, it should take backup with date, time. i need such software so that even if i delete some code which is important, i should be able to recover by seeing the history of changes made.
please help me with that.
karthikeyan,
What you’re describing is revision control software (also called version control or source control software.)
http://en.wikipedia.org/wiki/Revision_control
Depending on what platform you’re on and what version of MATLAB you’re using, you may be able to directly interface with an installed revision control software application:
http://www.mathworks.com/help/techdoc/matlab_env/f7-5297.html
or you may have the revision control software running outside MATLAB.
How can I get the Contents TODO & Help reports to look into subfolders that are collections (i.e. prefixed with +) ?
@JG,
There’s currently no option for this. The reports predate packages, and have not caught up yet. Thanks for your interest, though; I’ll let the reports team know.