Rename File Question/Possible Bug?

3 views (last 30 days)
Nick
Nick on 12 Jul 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

Accepted Answer

Jan
Jan on 12 Jul 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 Comment
Nick
Nick on 12 Jul 2012
Aha yes this works perfectly! Thank you! I guess I'll try to use full path names more often.

Sign in to comment.

More Answers (0)

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!