- 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
Unable to save the autoplot image in the for loop
1 view (last 30 days)
Show older comments
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 Comment
DGM
on 4 Jul 2023
Edited: DGM
on 4 Jul 2023
Well:
maybe you should let us know what's actually in the files and what the actual errors are.
Answers (1)
DGM
on 4 Jul 2023
Edited: DGM
on 4 Jul 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:
See Also
Categories
Find more on Environment and Settings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!