How to use strcat in imwrite to save multiple figures

2 visualizaciones (últimos 30 días)
Kelsey Ward
Kelsey Ward el 24 de Oct. de 2023
Comentada: Walter Roberson el 25 de Oct. de 2023
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.

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Oct. de 2023
See exportgraphics()
  5 comentarios
DGM
DGM el 25 de Oct. de 2023
Editada: DGM el 25 de Oct. de 2023
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 el 25 de Oct. de 2023
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

Iniciar sesión para comentar.

Categorías

Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by