Borrar filtros
Borrar filtros

Info

La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.

Loop that names first 3 files one name and 2nd 3 files another.

1 visualización (últimos 30 días)
JM
JM el 14 de Nov. de 2018
Cerrada: MATLAB Answer Bot el 20 de Ag. de 2021
Having a tough time doing something that is likely rather simple.
I have a folder with files, lets say they are named AAA.XLS, BBB.XLS, CCC.XLS.
I want to take half of the AAA.XLS files and rename them DDD.XLS. The files are stored so that there are 3AAA files in a row that will be kept AAA, followed by 3 files in a row that should be changed to DDD.
I have the code correct ( I believe) other than the for loop. I know how to loop every nTH iteration, but I want to do it in chunks so to speak with 3 on/3off.
Below is the code without the for loop
A =dir( fullfile(a, '*.xlsx'));
fileNames = { A.name };
for iFile = 1 : numel( A )
newName = fullfile(a, sprintf( '%03AAA.xlsx', iFile ) );
movefile( fullfile(a, fileNames{ iFile }), newName );
end

Respuestas (1)

Mark Sherstan
Mark Sherstan el 14 de Nov. de 2018
Editada: Mark Sherstan el 14 de Nov. de 2018
If I understand your question correctly try implementing one of the two codes to solve your problem:
for i = 1:numel(A)
if mod(i,3) == 0
doSomething(i);
continue
end
doSomethingElse(i);
end
for i = 1:3:numel(A)
doSomething(i);
end
  2 comentarios
JM
JM el 16 de Nov. de 2018
Editada: JM el 16 de Nov. de 2018
Thanks for the help.
This code gives me 1, 4, 7, 10, etc when I am interested in getting 1,2,3,7,8,9,etc basically 3 on 3 off if that makes sense.
Appreciate any help you can offer
Mark Sherstan
Mark Sherstan el 16 de Nov. de 2018
Sorry for the confusion! Here is a working correction:
flag = true;
for ii = 1:20
if flag == true
% Put code here
fprintf('%0.0f\n',ii)
end
if mod(ii,3) == 0
flag = ~flag;
end
end
Output is:
1
2
3
7
8
9
13
14
15
19
20

La pregunta está cerrada.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by