How can I save the matlab workspace in a structure?
Show older comments
I have a program that modifies a lot of variables in the workspace in every iteration. At the end of every iteration I need to check the old as well as the updated values of some of the variables in the workspace. Is there a way for me to do this by saving the current workspace in a structure and then comparing the current workspace with the structure?
Accepted Answer
More Answers (3)
Alexander Venus
on 27 Feb 2019
w = whos;
for a = 1:length(w)
str.(w(a).name) = eval(w(a).name);
end
1 Comment
Well, the cheesy and simple way to do it is,
S=load(save(dummyFilename));
However, I am somewhat skeptical that you're going down the right path with this. If all your variables are scalars, you should really be storing them concatenated in a vector and using vectorized functions to do all the comparisons.
2 Comments
Gaurav Pandey
on 1 Feb 2015
Image Analyst
on 1 Feb 2015
You accepted an answer, so are you still having trouble or not? I'm guessing not.
By the way, I do this all the time. I put the status of all my GUI widgets into a structure called UserSettings, and then save that when the app exits. Then when the user runs the app again, I retrieve that and set up the GUI just the way it was the last time they ran it - all the same settings.
Image Analyst
on 1 Feb 2015
Edited: Image Analyst
on 1 Feb 2015
You can save the variables in a structure
s.var1 = var1;
s.var2 = var2;
% Save to disk if you want
save(fullFileName, 's');
You can retrieve from disk, if you need to
s = load(fullFileName);
var1prior = s.var1;
var2prior = s.var2;
You can then compare variable by variable. I don't believe there is a way to compare a whole structure at once because each field can be vastly different data types.
if var1prior ~= var1
warndlg('var1 changed!');
end
if var2prior ~= var2
warndlg('var2 changed!');
end
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!