What's the correct syntax if you want to name the files dynamically when using exportgraphics?
I have a cell array A with 16 titles in each columns that are assigned to the colorbar string. I want to use the same titles to name the files.
for k = 1:16
%rest of the code
c = colorbar;
c.Label.String = string (A (:,k));
exportgraphics(gcf, string (A(:,k)) '.png' ,'Resolution',DPI) %this doesn't work
end

 Réponse acceptée

Ive J
Ive J le 22 Mar 2022
% A = {'name1', ...}
for k = 1:16
%rest of the code
c = colorbar;
c.Label.String = A{k};
exportgraphics(gcf, string(A{k}) + ".png" ,'Resolution',DPI) %this doesn't work
end
You could also use strcat, join or similar functions.

10 commentaires

Pelajar UM
Pelajar UM le 22 Mar 2022
Modifié(e) : Pelajar UM le 22 Mar 2022
Perfect! Is there a way to avoid uifigure from popping out every time you try to export the plot? I close it at the end of the loop but it would be good if it doesn't show up in the first place.
You can set visible property of figure to off when creating one, and close if afterwards:
for
fig = figure('Visible', 'off');
ax = axes(fig);
plot(ax, 1:10); % plot something
exportgraphics(fig, 'test.png')
close(fig)
end
Wonderful. Thank you so much. On a related note, what if I want to output the same loop to some UIAxes:
s = trisurf(F,P(:,1),P(:,2),P(:,3),app.UITable4.Data(:,k+5),'Parent', app.UIAxes40_k);
app.UIAxes40_k and app.UIAxes40_(k) don't work.
Not sure about that (not experienced with apps), but this works:
ax = uiaxes;
[x,y] = meshgrid(1:15,1:15);
z = peaks(15);
T = delaunay(x,y);
trisurf(T,x,y,z, 'parent', ax)
Yes, it works without the "k". I mean how do you incorporate k (k represents the loop) in the title of the UIAxes?
for k = 1:11
s = trisurf(F,P(:,1),P(:,2),P(:,3),app.UITable4.Data(:,k+5),'Parent', app.UIAxes40_k);
set(s,'LineWidth',0.1);
s.EdgeColor = 'none';
colormap jet;
c = colorbar;
c.Label.String = A{k};
end
Ive J
Ive J le 22 Mar 2022
Do you have multiple axes or only one axis that you like to update it within the loop? In case of one axis, you don't need to pass the k counter within the loop (it's just one single axis: app.UIAxes).
Pelajar UM
Pelajar UM le 22 Mar 2022
I have multiple axes. Basically the plots that I am exporting, I want to show them all next to each other (11 separate axes with names app.UIAxes40_1, app.UIAxes40_2, and so on....
So you need to pass the corresponding axis tag to trisurf:
for ...
s = trisurf(..., 'Parent', app.("UIAxes40_" + k);
end
Pelajar UM
Pelajar UM le 22 Mar 2022
Exactly what I was looking for. Thanks a lot!
Ive J
Ive J le 22 Mar 2022
Glad it works
cheers!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by