Is it possible to iteratively increase a number within the middle of a file name?

4 次查看(过去 30 天)
I have files which are named such that the identifying information is repeated in several places within the filename, and I want to automatically edit the filename each time to code loops through. The specific code I want to change is
Right1 = xlsread('C:\Documents\mouse 5\ 5 Comp1_right_Statistics\ 5 Comp1_right_Cell_Number_Of_Vesicles_VesicleType=Vesicles.csv','A:A');
to
Right1 = xlsread('C:\Documents\mouse 6\ 6 Comp1_right_Statistics\ 6 Comp1_right_Cell_Number_Of_Vesicles_VesicleType=Vesicles.csv','A:A');
automatically such that the next time, all the bolded 5s are 6, then 7, etc. Is there a filler variable or something I can put into the middle of a filename so I can increase the value of that variable and it will automatically insert the new number into it in multiple places (none of which are at the end of the name, unfortunately)?

采纳的回答

James Tursa
James Tursa 2018-8-9
编辑:James Tursa 2018-8-9
E.g., using sprintf
n = some integer
sprintf('Myfilename%dwith%dand%dstuff',n,n,n)
Sample run:
>> n = 5;
>> sprintf('Myfilename%dwith%dand%dstuff',n,n,n)
ans =
Myfilename5with5and5stuff
Put appropriate variation of that into your xlsread. If you will be running into integers more than one digit, you might want to specify a fixed number of digits to use for you string. E.g., instead of %d use %02d or %03d etc. That way the names will arrange nicely in directory lists.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by