Clear Filters
Clear Filters

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

4 views (last 30 days)
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)?

Accepted Answer

James Tursa
James Tursa on 9 Aug 2018
Edited: James Tursa on 9 Aug 2018
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.

More Answers (0)

Categories

Find more on Entering Commands in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!