Unable to save the autoplot image in the for loop

1 visualización (últimos 30 días)
amir
amir el 4 de Jul. de 2023
Comentada: amir el 5 de Jul. de 2023
My question is, suppose I have several thousands of mat file data and I want to read them first and then plot them and save the output as a photo image. I encountered a problem in the automatic saving section in the second loop, what should I do??
clc
clear
close all
for i=1:1
number=i+3;
f=num2str(number);
print1=strcat(f);
print=strcat('C:\Users\MASHHADSERVICE\OneDrive\Desktop\armina\data\',f,'.mat');
load(print)
c=cell2mat(Y);
eval(['class', print1 , ' =c;']);
end
for i=1:1
figure(i)
g=eval(['class', print1]);
plot(g)
print3=strcat('a',num2str(i+5),'.png');
saveas(figure(i),print3,'png')
end
  1 comentario
DGM
DGM el 4 de Jul. de 2023
Editada: DGM el 4 de Jul. de 2023
Well:
  • the code is uncommented and loads any number of unknown things into memory
  • you're abusing eval() to unnecessarily create dynamic variables
  • you're shadowing common function names which you may or may not be using elsewhere
  • the loops only iterate exactly once, and are therefore either superfluous or erroneous
  • you haven't mentioned at all what the actual problem is
maybe you should let us know what's actually in the files and what the actual errors are.

Iniciar sesión para comentar.

Respuestas (1)

DGM
DGM el 4 de Jul. de 2023
Editada: DGM el 4 de Jul. de 2023
clc
clear
close all
pathprefix = 'C:\Users\MASHHADSERVICE\OneDrive\Desktop\armina\data\';
numfiles = 1;
C = cell(numfiles,1);
for i = 1:numfiles
% this assumes the numbers have no leading zeros
infname = sprintf('%d.mat',i+3);
% i'm assuming each file contains only a cell array called Y
% and that the cell array is convertible to a numeric array
S = load(infname)
C{k} = cell2mat(S.Y);
end
for i = 1:numfiles
% create an unmanageable pile of windows
figure(i)
plot(C{k})
% consider using zero padding on filenames
outfname = sprintf('a%05d.png',i+5);
saveas(figure(i),outfname)
end
If the only thing that needs to be done is plotting, then the two loops can be combined and the entire cell array is unnecessary. In fact, you probably should do that, since it would be ridiculous to open "several thousand" figure windows at once. There would be no good reason to do such a thing.
clc
clear
close all
pathprefix = 'C:\Users\MASHHADSERVICE\OneDrive\Desktop\armina\data\';
numfiles = 1;
C = cell(numfiles,1);
for i = 1:numfiles
% this assumes the numbers have no leading zeros
infname = sprintf('%d.mat',i+3);
% i'm assuming each file contains only a cell array called Y
% and that the cell array is convertible to a numeric array
S = load(infname)
% create one figure window and reuse it
figure(1); clf
plot(cell2mat(S.Y))
% consider using zero padding on filenames
outfname = sprintf('a%05d.png',i+5);
saveas(figure(1),outfname)
end
See also:
  1 comentario
amir
amir el 5 de Jul. de 2023
You didn't understand what I meant, or I told you wrongly, but anyway, I solved my problem by myself using my own code.
This code was just the basics and I asked my own questions for a big project
Unable to post original code here
thank you

Iniciar sesión para comentar.

Categorías

Más información sobre Environment and Settings en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by