How to save in a sequence of files each output of a "for" loop

10 ビュー (過去 30 日間)
Katerina F
Katerina F 2014 年 9 月 12 日
コメント済み: Star Strider 2014 年 9 月 14 日
A have a number of txt files with data, and names like ABR1998, ABR1999 etc. I am running a script that includes a "for" loop to manupulate these files and I want each time the output is saved in txt files with the same (or similar name) e.g. ABRa1998,ABRa1999 etc. The output of each loop is the "A" (see below) which is a matrix with several columns and rows of data. Could you please tell me how to do this? Thanks-K.
matfiles=dir(fullfile('N:','My Documents','MATLAB','runfilesABR', '*.txt'));
for n=1998:2005
filename = ['ABR', int2str(n), '.txt'];
A=load(filename);
XXXXXXXXXX
XXXXXXXXXX
[d,c] = find(isnan(A));
A(d,:) = [];
A
end

採用された回答

Star Strider
Star Strider 2014 年 9 月 14 日
In the function forms of save and load, all arguments have to be strings:
filename = ['ABRa', int2str(n), '.txt'];
save(filename, 'A', '-ascii');
  2 件のコメント
Katerina F
Katerina F 2014 年 9 月 14 日
Many thanks it works. K.
Star Strider
Star Strider 2014 年 9 月 14 日
My pleasure!

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

その他の回答 (1 件)

Guillaume
Guillaume 2014 年 9 月 12 日
編集済み: Image Analyst 2014 年 9 月 12 日
save(filename, A, '-ascii'); %change filename if you wish
I don't particularly understand why you're getting the list of file in the runfilesABR directory and then never you that list, though.
Can you write the code you used to generate the new filename? As the message tells you it's not a string.
  4 件のコメント
per isakson
per isakson 2014 年 9 月 14 日
Add the lines
filename
whos A
before save or put a breakpoint at save to verify that they have appropriate values
Katerina F
Katerina F 2014 年 9 月 14 日
I tried it. It shows the same error message

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

カテゴリ

Help Center および File ExchangeMATLAB Compiler についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by