How to use strcat in imwrite to save multiple figures

1 次查看(过去 30 天)
I am trying to save figures in a loop using strcat and imwrite but I get the following error:
Error using imwrite (line 442)
Expected DATA to be one of these types:
double, single, uint8, uint16, uint32, uint64, int8, int16, int32, int64,
logical
Instead its type was matlab.graphics.chart.primitive.Line.
This is a subset of the code I am using to make the figures:
nfile = 'filename.nc';
z = [];
T = [];
L = [];
for j = 5:60;
nfile = strcat('folder_location/40f17_',num2str(j,'%02d'),'_1.nc');
z = ncread(nfile, 'z');
T = ncread(nfile, 'T');
L = ncread(nfile, 'L');
MeansL = mean(L,'omitnan');
MeansT = mean(T,'omitnan');
MeansZ = mean(z,'omitnan');
n_L = length(MeansL);
n_T = length(MeansT);
n_z = length(MeansZ);
time_L = 1:n_L;
time_T = 1:n_T;
time_z = 1:n_z;
plotL = plot(time_L,MeansL);
imwrite(plotL,strcat('folder_location/L40f17_',num2str(j,'%02d'),'.png'));
plotT = plot(time_T,MeansT);
imwrite(plotT,strcat('folder_location/T40f17_',num2str(j,'%02d'),'.png'));
plotZ = plot(time_z,MeansZ);
imwrite(plotZ,strcat('folder_location/z40f17_',num2str(j,'%02d'),'.png'));
end
It's just simple line graphs that I am trying to save directly as png files.

回答(1 个)

Walter Roberson
Walter Roberson 2023-10-24
See exportgraphics()
  5 个评论
DGM
DGM 2023-10-25
编辑:DGM 2023-10-25
You might be taking my earlier advice a little too severely. My point was to discourage using figure capture when your inputs are strictly raster images. When you're trying to capture graphics objects (line plots, charts, etc), it's appropriate to use exportgraphics(), saveas(), etc.
exportgraphics(gca,'myfilename.png') % just the axes
or
exportgraphics(gcf,'myfilename.png') % the figure
Walter Roberson
Walter Roberson 2023-10-25
nfile = 'filename.nc';
z = [];
T = [];
L = [];
ax = gca;
for j = 5:60;
nfile = strcat('folder_location/40f17_',num2str(j,'%02d'),'_1.nc');
z = ncread(nfile, 'z');
T = ncread(nfile, 'T');
L = ncread(nfile, 'L');
MeansL = mean(L,'omitnan');
MeansT = mean(T,'omitnan');
MeansZ = mean(z,'omitnan');
n_L = length(MeansL);
n_T = length(MeansT);
n_z = length(MeansZ);
time_L = 1:n_L;
time_T = 1:n_T;
time_z = 1:n_z;
plot(ax, time_L,MeansL);
exportgraphics(ax,strcat('folder_location/L40f17_',num2str(j,'%02d'),'.png'));
plot(ax, time_T,MeansT);
exportgraphics(ax, plotT,strcat('folder_location/T40f17_',num2str(j,'%02d'),'.png'));
plot(ax, time_z,MeansZ);
exportgraphics(ax, plotZ,strcat('folder_location/z40f17_',num2str(j,'%02d'),'.png'));
end

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by