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

 Akzeptierte Antwort

Ive J
Ive J am 22 Mär. 2022

1 Stimme

% 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 Kommentare

Pelajar UM
Pelajar UM am 22 Mär. 2022
Bearbeitet: Pelajar UM am 22 Mär. 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.
Ive J
Ive J am 22 Mär. 2022
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.
Ive J
Ive J am 22 Mär. 2022
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 am 22 Mär. 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 am 22 Mär. 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....
Ive J
Ive J am 22 Mär. 2022
So you need to pass the corresponding axis tag to trisurf:
for ...
s = trisurf(..., 'Parent', app.("UIAxes40_" + k);
end
Pelajar UM
Pelajar UM am 22 Mär. 2022
Exactly what I was looking for. Thanks a lot!
Ive J
Ive J am 22 Mär. 2022
Glad it works
cheers!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Creating, Deleting, and Querying Graphics Objects finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 22 Mär. 2022

Kommentiert:

am 22 Mär. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by