Borrar filtros
Borrar filtros

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

4 visualizaciones (últimos 30 días)
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)?

Respuesta aceptada

James Tursa
James Tursa el 9 de Ag. de 2018
Editada: James Tursa el 9 de Ag. de 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.

Más respuestas (0)

Categorías

Más información sobre Entering Commands en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by