Saving figures into structure?

조회 수: 29 (최근 30일)
Jared
Jared 2020년 6월 7일
답변: Mohammad Alhashash 2021년 9월 24일
Is it possible to save a figure into a structure? I'm trying to figure out how to save both data and figures into a single entity that I could then load in matlab. I know data can be saved in .mat and figures can be saved as .fig but is there not a way to save both in a single container?
  댓글 수: 2
Andreas Bernatzky
Andreas Bernatzky 2020년 6월 7일
Hey Jared,
look here:
data = rand(10,2);
myStruct.structFig = plot(data);
myStruct.structData = data;
should do the job
Jared
Jared 2020년 6월 7일
편집: Jared 2020년 6월 7일
Thanks Andreas. That's exactly what I was looking for. Follow up question...let's say I now have "structFig" in the structure but I close the actual figure in MATLAB. I now want to reopen the figure to see the actual figure. When I type:
myStruct.structFig
This doesn't work. All that does is tell me the structure i.e.,:
ans =
2×1 Line array:
Line
Line
I've tried open figure and that doesn't work either.
Thanks,
Jared

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

채택된 답변

Andreas Bernatzky
Andreas Bernatzky 2020년 6월 7일
Hey Jared,
data = rand(10,2);
myStruct.structFig = plot(data);
myStruct.structData = data;
m = figure(2);
% does not actually work
% % set(m,myStruct.structFig.XData,'XData');
you should have a look at the set() command. I haven't found a solution yet. Easiest would be just plot the data again but here you loose all of your preset defines like thickness and such things.

추가 답변 (2개)

Mohammad Alhashash
Mohammad Alhashash 2021년 9월 24일
Yes you can by simply save the plotting command (plot, bar, ...) as a string, and then use eval command to excute it. However, keep in mind the the input arguments of the plotting command have to be stored as vectors in other fields in the same structure.
for example
% lets make a 2D plot of x and y values
s.x = [1, 2, 3];
s.y = [4, 5, 6];
s.figure = "plot(s.x, s.y)"
s = struct with fields:
x: [1 2 3] y: [4 5 6] figure: "plot(s.x, s.y)"
% to plot the figure later
eval(s.figure)

Ameer Hamza
Ameer Hamza 2020년 6월 7일
Once a figure is closed, the associated data will be deleted and cannot be directly recovered. The only workaround is to create a class handle class with a transient property, as shown in my answer here: https://www.mathworks.com/matlabcentral/answers/521538-how-can-i-save-a-3d-rendering-as-a-fig

카테고리

Help CenterFile Exchange에서 Structures에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by