MATLAB Answers

Increment file name by adding numbers in a loop

조회 수: 907(최근 30일)
Amy
Amy 11 Jul 2011
댓글: Steven Lord 19 Feb 2018
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

  댓글 수: 0

로그인 to comment.

채택된 답변

Chirag Gupta
Chirag Gupta 11 Jul 2011
편집: Stephen Cobeldick 17 Feb 2017
Easy way to increment your filename is:
filename = sprintf('%s_%d','filename',k)

  댓글 수: 0

로그인 to comment.

추가 답변(1개)

Fangjun Jiang
Fangjun Jiang 11 Jul 2011
BaseName='MyDataFile_';
for k=1:10
FileName=[BaseName,num2str(k)]
end

  댓글 수: 5

표시 이전 댓글 수: 2
Alberto Rocha
Alberto Rocha 17 Feb 2017
Thanks, Stephen!
Mohammed Aliakbar
Mohammed Aliakbar 19 Feb 2018
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 19 Feb 2018
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.

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.


Translated by