Call Graphics Array to Make Figures Later In Code

6 次查看(过去 30 天)
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');

更多回答(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 个评论
Kate P
Kate P 2023-11-15
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(:);
Steven Lord
Steven Lord 2023-11-15
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Graphics Object Programming 的更多信息

标签

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by