Rename File Question/Possible Bug?

9 visualizaciones (últimos 30 días)
Nick
Nick el 12 de Jul. de 2012
I'm trying to have matlab copy a file, but change the name in the process. I believe I'm doing it exactly as i should, but I'm not getting the expected results. I'll try to be as detailed as possible.
I have a folder called modes that has a bunch of .png files in it. All the files are named something like 001_mode1234.png etc. Each one is unique. I want to copy a specific one of these files, move it up a folder, and rename it "runmode.png". So the code looks something like this:
indexmode = 1234
name = strcat('*_mode',num2str(indexmode),'.png');
cd modes/
copyfile(name,'../runmode.png')
cd ..
Now the result I'm getting is that in the main folder where the file should be copied to, instead of being renamed to "runmode.png" it is creating a folder called "runmode.png" and placing the image file (still named "001_mode1234.png") inside this folder.
I have tried using movefile, as well as tried copyfile(name,'runmode.png') to keep it in the same directory and both result in the same thing. Am I doing something wrong? Or is this possibly a bug? In case it matters I'm running matlab2012a on ubuntu 10.04

Respuesta aceptada

Jan
Jan el 12 de Jul. de 2012
indexmode = 1234;
folder = '/user/modes';
name = fullfile(folder, sprintf('*_mode%d.png', indexmode));
list = dir(name);
match = list.name; % There should be only one
copyfile(fullfile(folder, match), fullfile(folder, '..', 'runmode.png'));
Does this work? Absolute paths are more secure than relative ones or changing the current directory.
  1 comentario
Nick
Nick el 12 de Jul. de 2012
Aha yes this works perfectly! Thank you! I guess I'll try to use full path names more often.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB 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