Rename File Question/Possible Bug?

8 ビュー (過去 30 日間)
Nick
Nick 2012 年 7 月 12 日
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

採用された回答

Jan
Jan 2012 年 7 月 12 日
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 件のコメント
Nick
Nick 2012 年 7 月 12 日
Aha yes this works perfectly! Thank you! I guess I'll try to use full path names more often.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by