I want to select data piecewise in a loop, and I want to be able to save the files as filename_number, where the number increases for each iteration of the loop. Is there an easy way to accomplish this?
The idea is: first selection, saved file name is file_1 second selection, saved file name is file_2 etc

 採用された回答

Chirag Gupta
Chirag Gupta 2011 年 7 月 11 日
編集済み: Stephen23 2017 年 2 月 17 日

6 投票

Easy way to increment your filename is:
filename = sprintf('%s_%d','filename',k)

その他の回答 (1 件)

Fangjun Jiang
Fangjun Jiang 2011 年 7 月 11 日

8 投票

BaseName='MyDataFile_';
for k=1:10
FileName=[BaseName,num2str(k)]
end

5 件のコメント

Alberto Rocha
Alberto Rocha 2017 年 2 月 17 日
And how do I use FileName to save data in disk? I tried
save FileName variable -ascii
but the file that is saved gets the name "FileName", and not "MyDataFile_1" or "MyDataFile_2" etc.
Stephen23
Stephen23 2017 年 2 月 17 日
編集済み: Stephen23 2017 年 2 月 17 日
@Alberto Rocha: you have to use function syntax, not command syntax:
Or, to put it simply, you need to use the parentheses:
save(FileName,'X') % for variable X
Alberto Rocha
Alberto Rocha 2017 年 2 月 17 日
Thanks, Stephen!
Mohammed Aliakbar
Mohammed Aliakbar 2018 年 2 月 19 日
Hi Fangjun, I tried this function and it work with me but when I tried to execute OpenSees.exe, it give me error as the following: BaseName='TS'; for k=1:3 FileName=[BaseName,num2str(k),'.tcl']; !OpenSees.exe FileName end and the message is as the following: couldn't read file "FileName": no such file or directory
Steven Lord
Steven Lord 2018 年 2 月 19 日
You can't use ! if the command you want the system to execute is going to be assembled using the contents of a variable. Use the system function instead, building the command you want the system to execute using concatenation (as you built FileName) or sprintf.

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

カテゴリ

ヘルプ センター および File Exchange で Introduction to Installation and Licensing についてさらに検索

質問済み:

Amy
2011 年 7 月 11 日

コメント済み:

2018 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by