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

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?

 採用された回答

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 件のコメント

Oh sorry, you said same plot.
change figure;plot(...) with:
figure %before loop.
hold on
plot(X,Y,colorMap{i}); %inside loop
aneps
aneps 2013 年 12 月 11 日
Showing error! Unable to read file. Error in reading A=load([PATH,'filenames{i}']);
I put path like this:
PATH='E:\Trapgeofiles\simion\Data\test folder\'
The .dat files are in the 'test folder'.
kjetil87
kjetil87 2013 年 12 月 11 日
編集済み: kjetil87 2013 年 12 月 11 日
My bad, there should not be quotes '' on fileNames{i} (since fileNames i is a char array, with quotes its seen as a string )
correct:
A=load([PATH,fileNames{i}]);
aneps
aneps 2013 年 12 月 11 日
again another error!: "Cell contents reference from a non-cell array object. " . What does it mean? :(
kjetil87
kjetil87 2013 年 12 月 11 日
編集済み: kjetil87 2013 年 12 月 11 日
It means you are trying to index with {i} on something that is not a cell. (Also in your first post you typed filenames with a small n, i used capital N ).
Make sure that
fileNames={files.name}; (<-- note brackets not parantheses)
and that
colorMap={...} % colors.
i have tested this now :P , so somewhere you forgot to use {}.
Example test script:
obj1=1:10;
obj2=4:15;
obj3=5:16;
save('file1.dat','obj1','-ASCII');
save('file2.dat','obj2','-ASCII');
save('file3.dat','obj3','-ASCII');
clear all;
PATH = [cd,'\'];
files=dir([PATH,'*.dat']) ;
fileNames={files.name};
fileNames=sort(fileNames);
nFiles=numel(fileNames);
colorMap={'r','b','g'};
figure;hold on;
for i=1:min(nFiles,10)
A=load([PATH,fileNames{i}]);
X=1:numel(A);
Y=A;
plot(X,Y,colorMap{i});
end
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 件)

カテゴリ

ヘルプ センター および File ExchangeCreating, Deleting, and Querying Graphics Objects についてさらに検索

タグ

タグが未入力です。

質問済み:

2013 年 12 月 11 日

編集済み:

2013 年 12 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by