フィルターのクリア

How to use strcat in imwrite to save multiple figures

1 回表示 (過去 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 Center および File ExchangeImages についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by