Call Graphics Array to Make Figures Later In Code

Hi,
I'm processing some data in a for loop. Becuase each dataset is a different size, I rewrite over the variables with each new loop rather than save the data in a new index of the variable. However, I would like to make multiple figures using data from all of the loops. I know how to this with a single figure, but to make multiple figures I think I need to save the plotting info and call it later outside of the for loop. I'm not sure how to do this though. Also, if you close the figure that's made when you initally write the plotting code, it deletes the graphics array data. I've written up a code below to try to explain what I'm trying to do. I'm hoping to get the last two figures in the code.
Thanks for any help.
%% troubleshooting
close all
exampledata = rand(10,4);
exampletime = (1:10);
%% I know how to make a single figure like this.
figure("Name", "I can do it this way")
hold on
for k = 1:width(exampledata)
data = k*0.5*exampledata(:,k);
plot(exampletime,data)
end
hold off
%% I don't know how to do this way.
figure("Name","New figure so it doesn't write over the first one")
hold on
plotarray = gobjects(width(exampledata),1);
plotarray2 = gobjects(width(exampledata),1);
for k = 1:width(exampledata)
data = k*0.5*exampledata(:,k);
plotarray(k) = plot(exampletime,data);
plotarray2(k) = plot(exampletime,data-rand(size(data)));
end
hold off
figure("Name", "I can't figure this way out first fig")
plotarray;
figure("Name", "I can't figure this way out second fig")
plotarray2;

 채택된 답변

Voss
Voss 2023년 11월 15일
"How can I combine those two for loops so that both figures are made using a single for loop?"
% random example data:
close all
exampledata = rand(10,4);
exampletime = (1:10);
% create two figures and store their axes:
figure("Name", "I can do it this way")
ax = gca();
figure("Name","New figure so it doesn't write over the first one")
ax(end+1) = gca();
% plot into each respective axes:
hold(ax,'on')
plotarray = gobjects(width(exampledata),1);
plotarray2 = gobjects(width(exampledata),1);
for k = 1:width(exampledata)
data = k*0.5*exampledata(:,k);
plotarray(k) = plot(ax(1),exampletime,data);
plotarray2(k) = plot(ax(2),exampletime,data-rand(size(data)));
end
hold(ax,'off');

댓글 수: 2

Yes, this is exactly what I was looking for, thanks @Voss!!!!
You're welcome!

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

추가 답변 (1개)

madhan ravi
madhan ravi 2023년 11월 14일
h = cell(width(exampledata), 1) % outside loop
for k = 1 : width(exampledata)
h{k} = fig("Name" + k);
% do what you want to do with your data , plot it , and save it if you want to using savefig(…)
end
%h{1} to access figure 1

댓글 수: 3

@madhan ravi I don't understand how to use your code to do what I want. It says "unrecognized function of variable "fig"".
When I use h{1} in the command window it doesn't access the figure, it just prints the lines in the graphics array. Can you please include more complete code demonstrating how to use this?
I'll reiterate my question: I want two figures total where each iteration of the for loop creates one line per figure.
exampledata = rand(10,4);
exampletime = (1:10);
h = cell(width(exampledata), 1) % outside loop
h = 4×1 cell array
{0×0 double} {0×0 double} {0×0 double} {0×0 double}
for k = 1 : width(exampledata)
h{k} = fig("Name" + k);
% do what you want to do with your data , plot it , and save it if you want to using savefig(…)
end
Unrecognized function or variable 'fig'.
close all
exampledata = rand(10,4);
exampletime = (1:10);
%% I know how to make a single figure like this.
figure("Name", "Fig 1")
hold on
for k = 1:width(exampledata)
data = k*0.5*exampledata(:,k);
plot(exampletime,data)
end
hold off
figure("Name", "Fig 2")
hold on
for k = 1:width(exampledata)
data = k*0.5*exampledata(:,k);
plot(exampletime,data-rand(size(data)));
end
hold off
How can I combine those two for loops so that both figures are made using a single for loop? if I initiate the figure inside of the for loop it will make a new figure for every iteration of the loop so I cannot initiate the two figures inside the for loop. Is there is a way to close and reopen figures or to save plotting syntax for use later in the code so that I can save the 2 lines of plotting code made in the for loop?
% This is my best attempt at this but it does not work
% this just makes a figure with all of the lines in 1 figure
hold on
for k = 1:width(exampledata)
data1 = k*0.5*exampledata(:,k);
data2 = data1-rand(size(data1));
plot1(k) = plot(exampletime,data1);
plot2(k) = plot(exampletime,data2);
end
hold off
% I can't call the plotting syntax later in the code so these figures are
% just blank
figure("Name", "Figure 3")
plot1(:);
figure("Name", "Figure 4")
plot2(:);
Rather than preallocating h as a cell array, preallocate it to hold graphics objects using the gobjects function.
hLines = gobjects(1, 5);
x = 0:360;
axis([0 360 -1 1])
hold on
for k = 1:5
hLines(k) = plot(x, sind(k*x), 'DisplayName', "sin(" + k + "x)");
end
legend show
Now to access one of the lines just index into hLines.
sind3x = hLines(3);
Is this the line for sind(3x)?
sind3x.DisplayName
ans = 'sin(3x)'
Does its Y data match the sine of 3*x degrees?
ydata = sind3x.YData;
norm(sind(3*x)-ydata) % Should be 0
ans = 0
Yes.
You could use the same approach for storing figure handles instead of line handles if you wanted. The array preallocated by gobjects can hold various types (maybe all types, I'm not 100% sure) of graphics objects. You could even put different types of graphics objects in the same array.
hLines(6) = gcf
hLines =
1×6 graphics array: Line Line Line Line Line Figure

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

카테고리

도움말 센터File Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

릴리스

R2023a

태그

질문:

2023년 11월 14일

댓글:

2023년 11월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by