필터 지우기
필터 지우기

Unable to save the autoplot image in the for loop

조회 수: 1 (최근 30일)
amir
amir 2023년 7월 4일
댓글: amir 2023년 7월 5일
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
DGM
DGM 2023년 7월 4일
편집: DGM 2023년 7월 4일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

DGM
DGM 2023년 7월 4일
편집: DGM 2023년 7월 4일
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
amir
amir 2023년 7월 5일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Other Formats에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by