How to use strcat in imwrite to save multiple figures
2 次查看(过去 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.
0 个评论
回答(1 个)
Walter Roberson
2023-10-24
See exportgraphics()
5 个评论
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
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 Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!