How to save a session on Matlab - matlab

I was trying to save my Matlab variables and environment into a session so that next time I can directly load them without repeating the codes. I know how to save the Workspace variables but their way of saving a session doesn't work. It's said to be File -> Save to a session. But I couldn't find the button "File".
Any thoughts?

OK Actually I figured out an alternative solution. I realized that I could save all the Workspace variables. But my variables are too huge. So I ended up saving all my commands in a .m script file.
So next time when I want to resume my work, I just type the name of that script file to run all the useful commands to create the working variables and set up the script paths I need.

Related

How to access previously run commands in MATLAB

I saved my command line by first clicking on it and pressing ctrl+s and MATLAB saves a .mat file.
Now I want to open it in a text form in order to remember what I did in my command line the other day.
Is there a way to do that?
Saving a .mat file from the command window saves all of the variables that currently exist within your workspace within a binary .mat file. There is no information about the commands that were used to generate these variables in this file format therefore it cannot be automatically extracted from another program.
If you need to get information about what commands were run, you can look at your command history to see this. If you need to programmatically access this file you can look in MATLAB's preference directory for the file named history.m or history.xml on newer versions.
type(fullfile(prefdir, 'history.m'))
If you need to keep track of what commands you run in the future, you can use diary at the top of your script or beginning of your session to log all commands and associated command line output to a plain-text log file which would then be accessible to other programs.
diary('mylogfile.txt')

renaming variable's name while working on my script, in matlab

Working on my script in Matlab, I want to rename a variable, but only the upcoming instances of it the current script.
I am familiar with Matlab rename option when pressing Shift+Enter.
But this changes all the instances of my 'new' variable in the whole script.
I want to change only the proceeding instances.
How could this be done? I also didn't find anything helpful in the Find&Replace window.
Copy the remaining portion of your code into a new .m file
Find and replace the variable you want to change.
Copy and paste it back into your original .m file.

Matlab - Force to Retain Breakpoints

I wish to know, if there is a way to force Matlab to retain all historically placed breakpoints -red dots which enable code debugging- in the Matlab Editor/Debugger inside functions, classes, etc. from one session to another, for example, and without being deleted with clear all commands.
It would be easy for debugging huge pieces of software while changes are introduced, and because Matlab sometimes simply shut down because of internal errors.
Thanks fellows.
dbstop is the cleaner solution. Just insert it in the place you want the debugging to stop and this will not be removed until you edit or comment the line.
You need to save the breakpoints and reload them in next session. You can use dbstatus to get a structure that contains information about all breakpoints and save it into a file:
s = dbstatus('-completenames');
save FILENAME s
and later retrieve them using dbstop
load FILENAME
dbstop(s);
You can automate it by including it in startup.m and finish.m files (create them on default user path if they don't exist).

saving data from workspace to different dirrectory in matlab

I have a loop which my main script run through that. I wounder to save some of my variables in different directory every time that my loop is running. I have used following script but its not working:
for i=1:size(whisk, 1);
my codes is here and it creates variables in my workspace like [format, measurements].
the rest is what I wote to save this variables:
mkdir('C:\videos\results\', num2str(i));
dumvar=0; % As matlab has problem with data>2GB, then I use this 2 line code for saving
save('measurenments','dumvar','-v7.3');
save(fullfile('C:\videos\results\', num2str(i),'measurenments'));
clear all;
close all;
end
but Unfortunately its not work!!!!!!!!!!!
Any help would be appreciated.
Sam
Except that measurenments is wrongly spelled (correct spelling is measurements), there is not so strange that it does not work. The first call to save, saves the variable dumvar in the current folder, with the format v7.3. The second call to save, saves the whole workspace as a file fullfile('C:\videos\results\', num2str(i),'measurenments'). Try this,
save(fullfile('C:\videos\results\', num2str(i),'measurenments'),'dumvar','-v7.3');
However it seems as the folder fullfile('C:\videos\results\', num2str(i),'measurenments') does not exist since you only create the folder mkdir('C:\videos\results\', num2str(i))
. Then matlab cannot save anything there. Try either to save as fullfile('C:\videos\results\', [num2str(i),'measurenments']) or create the directory mkdir('C:\videos\results\', [num2str(i),'\','measurenments']);
`

Retrieve Variables/Accesing Variables in different Workspaces

I imported some data and them cleaned it up, there is a lot of data so this is quite time consuming.
I then wrote a scipt that uses these variables.
However when I went to run the script, it says that these varibles are not know. In the workspace editor is only shows variables declared in the script.
Q1.) Is there a way to get the original variables back?
Secondly as the clean up operation takes quite some time I don't want to have to import that data and clean in every time I run the M-file.
Q2.) So once I have the cleaned data how do I make these variables available to an M file (do I have to declare them as global or something like that?)
To be clear I tried to just debug the script rather than running it from the command window, I just wnat to get the base variables back now if possible!
ahem there was a errant clear used in the script that's why all the variables were disappearing.
A misplaced Clear or Clear all may be the problem. Similar thing happened to me once.