Borrar filtros
Borrar filtros

Naming the folders with a counter

1 visualización (últimos 30 días)
Rohan Maharjajn
Rohan Maharjajn el 27 de Ag. de 2017
Editada: Stephen23 el 27 de Ag. de 2017
I used the following code to move the generated files to a new folder for every new execution.It works for the command interface .But since I am trying to create a GUI and '.exe' file for the codes, I am having problem creating a new folder with every execution as the counter value is not updated in the program memory.
c=xlsread('counter.xlsx');
f=c(1)+1;
newdir = sprintf('Problem_%d', c);
mkdir(fullfile(newdir))
movefile ('BendingMoment.png', newdir);
movefile ('ShearForce.png', newdir);
movefile ('Stress.png', newdir);
movefile ('shaft_data.xlsx', newdir);
xlswrite('counter.xlsx',f);

Respuesta aceptada

Walter Roberson
Walter Roberson el 27 de Ag. de 2017
Editada: Walter Roberson el 27 de Ag. de 2017
You have to ask yourself which directory you are in at the time you execute the xlsread() and the other statements. In a compiled executable, chances are that you are in ctfroot() and that all action is taking place there.
If the original counter.xlsx is saved as part of the compiled executable, then each time the executable were re-expanded then the file would get overwritten.
You should probably be considering detecting the directories already present instead of relying on a counter like that. Detecting directories would be a little easier if you used zero-padded counters, such as Problem_00018 instead of Problem_18 . For that,
newdir = sprintf('Problem_%05d', c);
I note that you are using movefile(): if those worked, then the files would not be there for a second execution. Perhaps you should be using copyfile() ?
  1 comentario
Stephen23
Stephen23 el 27 de Ag. de 2017
Editada: Stephen23 el 27 de Ag. de 2017
"You should probably be considering detecting the directories already present instead of relying on a counter like that."
I have no idea if it works with the MATLAB coder, but my FEX submission does exactly that: checks if files/folders exist, and returns the next unused name:
It also correctly handles any leading zeros in the existing names, and lets the user define if the next name should include leading zeros as well.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Stability Analysis en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by