How to run a code for several files also how to stop the code for a specific number of files and give output?

조회 수: 2 (최근 30일)
I have following code:
A=load('CurRunA.dat');
X=A(:,1); Y=A(:,3);
figure( plot(X,Y)
I want to run this code for several files CurRunA, CurRunB, CurRunC..... How can I run this code automatically for all the files like these and plot the X,Y curve for all those data in a single plot with different colors?
I can rename those files for the sake of programing. Also, if I have 20 files and if I want to do this only up to 10 files, how can I stop the program till 10 and provide the output?

채택된 답변

kjetil87
kjetil87 2013년 12월 11일
PATH = 'C:\myFilesAreHere\'; %remember the last \
files=dir([PATH,'*.dat']) ; %get info about all files in PATH that ends with .dat
fileNames={files.name}; %extract only filenames, put them in a cell.
%im not sure if the filenames are allredy sorted or not but to be sure:
fileNames=sort(fileNames);
nFiles=numel(fileNames); %count number of files
colorMap={'red','blue','green',... etc} % if your run out of colors use e.g '-.red'
for i=1:min(nFiles,10) %run for either 1:10 or 1:nFiles if nFiles<10.
A=load([PATH,'fileNames{i}']);
% your code
figure;plot(X,Y,colorMap{i} );
end
%%%%%%% END %%%%%
This ok? =)
  댓글 수: 7
aneps
aneps 2013년 12월 11일
Yes, thank you. I put fileNames=(files.name)! OK. But now I am getting only one plot. I put
figure
hold on (before the for loop) and
plot(X,Y,colorMap{i}); (inside the loop).
But still I am getting only one plot!
kjetil87
kjetil87 2013년 12월 11일
편집: kjetil87 2013년 12월 11일
Do you want one figure for each A? if so put figure inside loop.
Otherwise makes sure you found all files, try a
disp(nFiles) in there
If nFiles>1 and you still only get one "plot" , check what color it has, maybe the contents of the files are all the same?

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Just for fun에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by