Borrar filtros
Borrar filtros

sprintf extra space character!

3 visualizaciones (últimos 30 días)
Tala Hed
Tala Hed el 10 de Abr. de 2018
Comentada: Tala Hed el 10 de Abr. de 2018
Experts, I am trying to rename some images with the following code. The new names should be '1.jpg', '2.jpg' etc. However, I get ' 1.jpg', ' 2.jpg' etc. Note there is a space before each digit. What is the issue with my code?
a ='C:\Users\Faraz\Desktop\IBHS\Flame height\Frames\';
A =dir( fullfile(a, '*.jpg') );
fileNames = { A.name };
for iFile = 1 : numel( A )
newName = fullfile(a, sprintf( '%2d.jpg', iFile ) );
movefile( fullfile(a, fileNames{ iFile }), newName );
end

Respuesta aceptada

Walter Roberson
Walter Roberson el 10 de Abr. de 2018
%2d means that it should always take a minimum of 2 characters for the number. %d would use the minimum length for each number.
If you were looking for 01.jpg, 02.jpg, ... 09.jpg, 10.jpg, etc., then you would use %02d
  5 comentarios
Walter Roberson
Walter Roberson el 10 de Abr. de 2018
a ='C:\Users\Faraz\Desktop\IBHS\Flame height\Frames\';
A = dir( fullfile(a, '*.jpg') );
fileNames = fullfile(a, {A.name} );
for iFile = 1 : length(fileNames)
oldName = fileNames{iFile};
newName = fullfile(a, sprintf( '%d.jpg', iFile ) );
if ~strcmp(oldName, newName)
movefile( oldName, newName );
end
end
Tala Hed
Tala Hed el 10 de Abr. de 2018
Thank you, Walter

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by