saving everything in the workspace

Hi, the save comand only saves the variables, handles, etc. However I need to save everything in the workspace including struct, doubles, etc...everything.
Also, once everything is saved I would like to load everything exactly the way it was saved. Would all this be possible?
Thank you

9 Comments

Do you think structures and doubles are not variables???
What struct or double exists in the workspace that is not inside a variable ?
Vincent I
Vincent I on 30 May 2013
Edited: Vincent I on 30 May 2013
ok. So before saving I have a multitude of variables(yes, i understand struct and doubles are variables). However whats confusing is that when I load the saved mat file it loads into the workspace as handles, hObject and eventdata. Thats why it's confusing and I dididnt really understand where all the struct, doubles etc went.
Idealy, I would like to, pretty much, save a copy or image of the workspace and load the same copy of the workspace(just like when you save an image of an hdd and reimage it onto another or same HDD). As of right now the way the mat file loads it would be close to imposible to figure everything out.
Does that makes sense?
I would be surprised if it only saved the handles, hObject, and eventdata and nothing else. But why save everything inside a callback? Why not save only the variables that you need to recall later? And it will only save local variables by default so where are you planning on recalling these variables? In the same function? Or a different function to "poof" those variables into existence?
because I'm working on a real time simulator. i would like to save the workspace and if desired later on to load the workspace and continue the simulation with the same parameters.
pretty much just like a game... save now in case you want to come back or you missed something you can just load the previous saved data and continue.
I will post some pictures of the saved and loaded mat file. But basicaly if initially the workspace has a multitude of variables when I load the mat file only 3 names show: eventdata,hObject and handles.
btw code use to save the file
function Save_Callback(hObject, eventdata, handles)
% hObject handle to Save (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[filename, pathname] = uiputfile(...
{'*.mat';'*.*'},...
'Save as');
save(fullfile(pathname, filename))
save in Save_Callback will save the variables in the workspace of Save_Callback. Wouldn't you need something like
evalin( 'caller', 'save...' )
whether smelly or not
per is correct.
At the point that you call save() you should have hObject, eventdata, handles, and filename, and pathname as the only variables in existence. It's a mystery to me why filename and pathname do not get saved into the mat file. The help file says "save(filename) stores all variables from the current workspace in a MATLAB® formatted binary file (MAT-file) called filename." So, if like you say, only the first three variables are being stored and able to be recalled and the latter two aren't, I'd call the Mathworks about it. But first see if you can open that mat file in any other script or from the command line and see what gets returned.
Vincent I
Vincent I on 31 May 2013
Edited: Vincent I on 31 May 2013
ok. yes, per is corect... just realized it.. I guess when the callback is called it only passes the handles hObject and eventdata. thats why save only saves whats in the workspace.
does anyone have any idea how to avoid this and save everything in the the workspace prior to calling the same Save_Callback function?
how do I use evalin( 'caller', 'save...' )??? the only way I know how to use evalin is by saving every variable( eg: evalin('caller',variable)) and that would be close to imposible when there are close to 50 variables in the workspace.

Sign in to comment.

 Accepted Answer

per isakson
per isakson on 31 May 2013
Edited: per isakson on 31 May 2013
You can use the command or the function syntax. If the variable names are the values of string variables then command syntax cannot be used.
Try
>> save_test
it prints to the command window
Name Size Bytes Class Attributes
a 1x1 8 double
where
function save_test
a = 1;
b = 2;
Save_Xallback
whos -file save_test.mat
end
function Save_Xallback
evalin( 'caller', 'save save_test.mat a' );
end
.
Answer to the first comment:
The above is a hint rather than a complete answer. I don't exactly understand what you want to achieve. Variables of which workspace do you want to save? Outline a simple example in code. Some basics:
Workspaces
  • there is the base workspace and
  • each function has it own workspace
Callbacks
  • callbacks are "called" from the base workspace; the caller is the base. Thus, evalin( 'caller', ... ) and evalin( 'base', ... ) are equivalent in a callback function.
Command and Function syntax
  • save myfile.mat a
  • save( 'myfile.mat', 'a' )
"I'm thinking ... : evalin('caller','save save_test.mat variables')"
  • The variables of which workspace do you want to save?
  • Why did you introduce the function, Save_Callback?
"... save the workspace while in any function?"
  • there is no "the workspace". There are many.
I believe it is difficult to save the complete "state" of a running Matlab program. Special cases maybe.

3 Comments

Vincent I
Vincent I on 31 May 2013
Edited: Vincent I on 31 May 2013
thank you for your answer however this might not work. Since the program runs in a while loop i have to use the "Save_Callback" function which is in the menu. Plus before the Save_Callback there are roughly a dozen variables and I really dont know how i can save all of them using evalin. I'm thinking that I should use who to store all the variables prior to calling the save function and then do something like this: evalin('caller','save save_test.mat variables') i have no idea if this will work...
Going back to my previous comment regarding saving a game and then loading the saved configuration. Whould it be posible to do the same with matlab, when you desire to save the workspace while in any function?
Any other sugestions? Thank you
OK so instead of using caller I used base
evalin('base','save final.mat') and it worked!!! :-)
thank you
See my answer.
My apologize, I was trying to be as clear as I could and not giving too much information because of the nature of the project. That being said I was able to solve the problem and I have a better idea now of what's going on between functions and how the workspace works. Initially I thought that once you assign a variable to the workspace it stays there even when you access a different function. Which obviously thats not the case.
However, I've just learned that by using "base" instead of "caller" in the evalin command I could save all the variables in the work space:
[filename, pathname] = uiputfile({'*.mat';'*.*'}, 'Save as');
evalin('base',char(strcat({'save '},filename)))
Why introducing the function Save_Callback? Save_Callback is a uimenu function that enables the user of the program to save the parameters that can be loaded back into the program later on if desired.
Thank you for your help

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!