exportgraphics dynamic file name

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

 Accepted Answer

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

Pelajar UM
Pelajar UM on 22 Mar 2022
Edited: Pelajar UM on 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
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).
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
Exactly what I was looking for. Thanks a lot!
Glad it works
cheers!

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 22 Mar 2022

Commented:

on 22 Mar 2022

Community Treasure Hunt

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

Start Hunting!