필터 지우기
필터 지우기

How can I save the matlab workspace in a structure?

조회 수: 99 (최근 30일)
Gaurav Pandey
Gaurav Pandey 2015년 2월 1일
편집: Matt J 2019년 2월 27일
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?

채택된 답변

Matt J
Matt J 2015년 2월 1일
편집: Matt J 2015년 2월 1일
If a struct is really what you want, you can automatically generate statements to build the struct, using STRUCTVARS ( Download ).
>> a=1, b=2, c=3
a =
1
b =
2
c =
3
>> z=[who,who].'; myStruct=struct(z{:}); structvars(myStruct,0)
ans =
myStruct.a = a;
myStruct.b = b;
myStruct.c = c;
The idea is just to copy/paste the assignment commands into your mfile and update them as needed.
  댓글 수: 9
Matt J
Matt J 2015년 2월 27일
I don't use Simulink, but I have a hard time seeing why, if you are able to execute,
myStruct.(names{j}) = eval(names{j});
you cannot just as easily, and at the same place in your code, execute
myStruct.a=a;
The two are absolutely equivalent when names{j}='a'. Obviously, you can do the same with all of your other variables.
Stephen23
Stephen23 2015년 2월 27일
편집: Stephen23 2015년 2월 27일
@Dani Tormo: you really should avoid using eval for such a trivial thing as allocating values to a variable. Structures are perfectly capable of storing your values in a loop, and Matt J has shown you exactly how to do this. If you want robust code, learn to avoid eval:

댓글을 달려면 로그인하십시오.

추가 답변 (3개)

Alexander Venus
Alexander Venus 2019년 2월 27일
w = whos;
for a = 1:length(w)
str.(w(a).name) = eval(w(a).name);
end
  댓글 수: 1
Matt J
Matt J 2019년 2월 27일
편집: Matt J 2019년 2월 27일
This solution was already discussed (and discouraged) here. Although, in hindsight, this is probably one of the safer applications of eval - no risk here of being shadowed by existing function names.

댓글을 달려면 로그인하십시오.


Matt J
Matt J 2015년 2월 1일
편집: Matt J 2015년 2월 1일
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
Gaurav Pandey
Gaurav Pandey 2015년 2월 1일
No, all the variables are matrices.
Image Analyst
Image Analyst 2015년 2월 1일
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
Image Analyst 2015년 2월 1일
편집: Image Analyst 2015년 2월 1일
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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by