Borrar filtros
Borrar filtros

Problem using move file

7 visualizaciones (últimos 30 días)
Annika
Annika el 16 de Jul. de 2014
Comentada: Annika el 16 de Jul. de 2014
Hi, I am new to MATLAB and trying to rename all the images inside a particular folder.I want to add a string hole with the names of my file.
if true
% code
end
path = 'U:\Thesis\COMSOL part\Various designs\With hole-Copy\';
dirData = dir(strcat(path,'*.png')); %# Get the selected file data
fileNames = {dirData.name}; %# Create a cell array of file names
for iFile = 1:numel(fileNames) %# Loop over the file names
newName = strcat('hole',fileNames,sprintf('%s',iFile)); %# Make the new name
movefile(fileNames{iFile},newName); %# Rename the file
end
But i get this error message "Error using movefile. Argument must contain a string."
Please help

Respuesta aceptada

Joseph Cheng
Joseph Cheng el 16 de Jul. de 2014
Editada: Joseph Cheng el 16 de Jul. de 2014
what you're missing is
path = 'C:\temps (IT do not delete)\matlabAnswers\dirtest\';
dirData = dir(strcat(path,'*.wav')); %# Get the selected file data
fileNames = {dirData.name}; %# Create a cell array of file names
for iFile = 1:numel(fileNames) %# Loop over the file names
newName = strcat('hole',fileNames(iFile),sprintf('%d',iFile)); %# Make the new name
movefile(fileNames{iFile},cell2mat(newName)); %# Rename the file
end
i used wav files as i had a directory full of junk wav files. just change to png.. so the initial problems are:
  1. in the move file the newName was a cell. that is why it asked for a string.
  2. in the sprintf you were printing the number as a string. so it made it an empty space after the new name.
I'm not exactly sure if you want to put the numbering after the .wav#. perhaps you wanted holeFilename#.wav
then you substitute the for loop with something like this
for iFile = 1:numel(fileNames) %# Loop over the file names
newName = strcat('hole',fileNames{iFile}(1:end-4),sprintf('%d',iFile),'.wav'); %# Make the new name
movefile(fileNames{iFile},newName); %# Rename the file
end
  1 comentario
Annika
Annika el 16 de Jul. de 2014
Thank you so much.

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