How can I iterate through a list of files using dlmread()?

2 ビュー (過去 30 日間)
Haydn Darragh
Haydn Darragh 2015 年 8 月 19 日
コメント済み: Haydn Darragh 2015 年 8 月 19 日
Hello,
I am able to load all files and manually change the name of the files to access each on individually, however when I try to use a for loop that changes the name of the file in the form of a changing string, I get the error message below:
"Error using dlmread (line 119) The file 'file1' could not be opened because: No such file or directory
Error in EbnoVsRange (line 19) Zero(i) = dlmread(fname,',',[26 2 26 2]);"
This is after I have loaded the files in, one of which is called 'file1'
The code I'm using is below:
file1 = 'C:\Users\....\range1cm.csv';
file2 = 'C:\Users\....\range2cm.csv';
file3 = 'C:\Users\....\range3cm.csv';
file4 = 'C:\Users\....\range4cm.csv';
file5 = 'C:\Users\....\range5cm.csv';
for i = 1:1:10
fname=['file',num2str(i)]
Zero(i) = dlmread(fname,',',[26 2 26 2]);
end
Thank You

採用された回答

David Legland
David Legland 2015 年 8 月 19 日
編集済み: David Legland 2015 年 8 月 19 日
Hi,
The problem comes from you ask Matlab to open the file called 'file1', not the file whose name is stored in the variable file1.
In your case you can use the 'eval' function:
fname = eval(['file' num2str(1)]);
However, I would rather build the file name programmatically:
inputDir = 'C:\Users\...'
fname = fullfile(inputDir, sprintf('range%dcm.csv', i));
  1 件のコメント
Haydn Darragh
Haydn Darragh 2015 年 8 月 19 日
This is great, thank you for your response. I have also implemented your other option for retrieving the files too. This will save many lines of code at the end of the day

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeBig Data Processing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by