Any way to get 2015a working with two environments?

3 views (last 30 days)
This is kind of a silly question of convenience, but I'll ask anyway.
I have two large matlab projects that do not overlap in code base. Matlab saves the status of your session, command history and open files and such. Of course then when I go to work on the other project, I have to close all these files and open those...
Is there a way to save the current sessions status say with a project name? So that I can open project A and work there. Close project A and then Open project B with a different set of open files and command history etc?
Thanks for all answers.

Accepted Answer

Cam Salzberger
Cam Salzberger on 4 Sep 2015
Hello David,
I understand that you would like to be able to save the multiple sessions of MATLAB, and be able to choose which to open. Unfortunately, there is no officially supported way of doing this easily. There may be unofficial ways that MathWorks cannot recommend, so it may be worth searching around for how other people restore previous sessions of MATLAB.
There are, however, several supported ways of saving and loading individual elements of a session. You could create a custom finish.m that would ask you to which "project" you want to save your components, and then saves them. Then you could have a custom startup.m that would ask you which "project" to load, and then load the previously saved components.
You can get a list of files that are open in the Editor using:
docArray = matlab.desktop.editor.getAll;
fNames = cell(1,length(docArray));
for fIdx = 1:length(docArray)
fNames{fIdx} = docArray(fIdx).Filename;
end
You can save this (along with other session stuff) in a MAT-file. When you next open MATLAB, you can load the MAT-file and open each document with:
for fIdx = 1:length(fNames)
matlab.desktop.editor.openDocument(fNames{fIdx});
end
You could even save the line the cursor was at, and use the openAndGoToLine function to move the cursor to that point again.
The Command History window is trickier. You can get the command history from your current session using the command:
historypath = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
This has been documented, but I am not aware of any documented function that does the reverse (appending to or modifying the command history).
The Workspace is the easiest, since you can just use the save command to throw all your variables into a MAT-file, and the load command to get them back again.
You could also save all open figures to a "project"-specific folder, and then reopen them (using the dir command to find files in that folder).
I hope this helps with maintaining your workflow.
-Cam

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!