Copy the value from a variable to a new name

Hi,
I have a not very difficult problem, but I don't know how to solve it. I've created a variable and I need to write this number (1, 2, 3 or 4) in the newfileName.
for k = 1:length(fileNames)
n = num2str(fileNames{k}(3:4));
if n > 20 & n < 21
fileName = 1 % put your file name for this case here
elseif n >= 22 & n < 23
fileName = 2 % put your file name for this case here
elseif n >= 24 & n < 25
fileName = 3 % put your file name for this case here
else
fileName = 4
end
newFileName = [ fileNames{k}(3) '_dm_' fileNames{k}(4:9) '_' fileNames{k}(10:16) 'hu' ];
movefile([myPath fileNames{k}], [myPath newFileName]);
end
For example, if I write the following code the result is not the value from this variable (1,2,3 or 4). It's "hufileName". How can I change it?
newFileName = [ fileNames{k}(3) '_dm_' fileNames{k}(4:9) '_' fileNames{k}(10:16) 'hu' 'fileName'
Thanks in advance and greetings,
Emma

 採用された回答

Robert Cumming
Robert Cumming 2012 年 10 月 19 日
編集済み: Robert Cumming 2012 年 10 月 19 日

0 投票

in your code - filename is a number so you have two choices
1. use num2str to change it from a number to a string
newFilename = [ 'string' num2str(filename) ];
2. make it expicitly a string
filename = '1';
You may also benefit from looking up
help fullfile

4 件のコメント

Emmanuelle
Emmanuelle 2012 年 10 月 19 日
編集済み: Emmanuelle 2012 年 10 月 19 日
Hi Robert, thanks for your quicky reply. I've tried with your steps but I always get an error at the movefile command. I've converted the number to a string doing this: S= {fileName} but now, when I runned the command 'movefile' I've received the following error: "??? Error using ==> movefile Argument must contain a string."
Robert Cumming
Robert Cumming 2012 年 10 月 19 日
why are you putting it in a cell array? I didn't suggest that..
Emmanuelle
Emmanuelle 2012 年 10 月 19 日
Sorry. I've tried this "n = ['string' num2str(fileNames{k}(4:9))];" and I get that error: ??? Error using ==> movefile. Also, when I change " fileName = '1';" I get the same error.
Emmanuelle
Emmanuelle 2012 年 10 月 19 日
I've done something similar, as you said. Thank you so much Robert. Greetings,

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by