Is it possible to iteratively increase a number within the middle of a file name?
4 views (last 30 days)
Show older comments
Kellie Wilson
on 9 Aug 2018
Commented: Kellie Wilson
on 9 Aug 2018
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)?
0 Comments
Accepted Answer
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)
See Also
Categories
Find more on Environment and Settings 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!