How to use strcat in imwrite to save multiple figures

조회 수: 2 (최근 30일)
Kelsey Ward
Kelsey Ward 2023년 10월 24일
댓글: Walter Roberson 2023년 10월 25일
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에서 Specifying Target for Graphics Output에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by