# Save and Load MATLAB Projects in the Editor4

Posted by Sean de Wolski,

Sean's pick this week is Save and load MATLAB "projects" in editor by Kevin Bartlett.

### What is matproj()?

From the help:

matproj.m--Saves and loads MATLAB "projects". A project consists of the
MATLAB files that are currently open in the MATLAB Editor. Loading a
project returns the MATLAB Editor to the state it was in when the project
was saved, with the same MATLAB files open to the same lines, the working
directory set to that of the loaded project and the MATLAB search path
set back to its saved value.

### Why do I like this?

I tend to be a little obsessive compulsive about having many windows open on my computer. This means I close figures, files, Internet Tabs, etc. that I am not using. For the purpose of closing editor files I have a little shortcut, CloseNoPrompt, that allows me to quickly and painlessly close all open MATLAB files.

   function CloseNoPrompt
       %All Editor files;
closeNoPrompt(matlab.desktop.editor.getAll);
   end

matproj allows me to save the current state of my editor so that I can continue to shamelessly close my files but be able to get back to the same state at a later time. This is really useful in day-to-day work where I may need many files open to work on a task but then will not need them for a few days while working on something else. Since matproj also stores the location of the cursor in the file, you can get right back to exactly where you want to be!

### The Gift That Keeps on Giving

I've been trying to break matproj by coming up with edge cases where it might fail. So far, it has outsmarted me every time. Here are a few of the other features that I've run into:

• Works with *.txt, and *.c files as well.
• If a file that is part of a saved project is deleted, it warns that this file is missing and opens the rest of the project.
• If a file is not saved, it stops instead of closing those files.
• Errors if you try to open a file that is not a project.

### Enhancement Requests

My main suggestion to improve this file is to package it into a MATLAB App. This would make it easy to access from the Apps tab. Of course you could add it as a shortcut too.

Okay, I've already turned it into an app and realized it comes with another very nice benefit: The app is now installed in a specific apps directory. Whenever a project is saved or loaded, this directory is the one opened, so the only available *.mat files are ones relevant to this app. This means I don't need to filter through any other ones that may be in the directory.

Another suggestion would be an option to not close all active files when loading a project. Right now, all current editor files are closed when a saved project is loaded. Although this behavior is expected, it would be nice to be able to keep the current files open as well. It would also make it easy to concatenate projects :)

Do you have a use for MATLAB Projects?

Get the MATLAB code

Published with MATLAB® R2013a

Thierry Dalon replied on : 1 of 4

There are other FEX contributions on the same topic:
http://www.mathworks.com/matlabcentral/fileexchange/23218-project-manager.
I prefer having all my projects saved in one single file (see other FEX projects implementation) instead of one file per project.
Advantage of matproj as you noticed is that is saves as well the selection/position.
What about posting your App version and the simple correction for merging in this Post?

% Close existing project – files opened in Editor
docHndl = matlab.desktop.editor.getAll();
numFiles = length(docHndl);
if numFiles>0,
answ=questdlg(‘Do you want to close files already opened in the Editor?’,’Close opened Files?’,…
‘Yes’,’No’,’Yes’);
drawnow
if strcmp(answ,’Yes’),
% closeNoPrompt(docHndl)
for iFile = 1:numFiles
docHndl(iFile).close;
end
end
end

Sean de Wolski replied on : 2 of 4

Hi Thierry,

To package the App, I literally just added matrpoj.m as the main file and selected an icon. It wasn’t enough work to warrant posting it.

I like your fix for merging :)

%Sean

Guest Picker replied on : 4 of 4

Hi Troy,

Thanks for pointing us to matlabState, it does look good. Unfortunately, it relies heavily on undocumented functionality which makes it ineligible for recognition here.

%Sean