Call Graphics Array to Make Figures Later In Code
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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;
0 commentaires
Réponse acceptée
Voss
le 15 Nov 2023
"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 commentaires
Plus de réponses (1)
madhan ravi
le 14 Nov 2023
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 commentaires
Steven Lord
le 15 Nov 2023
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
Does its Y data match the sine of 3*x degrees?
ydata = sind3x.YData;
norm(sind(3*x)-ydata) % Should be 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
Voir également
Catégories
En savoir plus sur Graphics Object Programming dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!