フィルターのクリア

"If sentence" error

1 回表示 (過去 30 日間)
Emmanuelle
Emmanuelle 2012 年 10 月 22 日
Hi,
I have a problem. I want to rename some files and this is my code. The problem is the if sentence doesn't work at all. The result is always 21 and that's not possible. I've tried to change the operand & to && but it doesn't work neither.
myPath = 'C:\EXERCICES\';
a= dir (fullfile(myPath,'*.zip));
fileNames = { a.name };
for k = 1:length(fileNames)
n = num2str(fileNames{k}(4:9));
if n >= 205040 && n < 208041
fileName = 18;
elseif n >= 203031 && n < 205030
fileName = 19;
elseif n >= 199031 && n < 202030
fileName = 20;
else n >= 196033 & n < 198033;
fileName = 21;
end
xs= num2str(fileName);
newFileName = [fileNames{k}(4:9) '_' fileNames{k}(10:16) xs '_gsd60_std' '.zip];
movefile([myPath fileNames{k}], [myPath newFileName]);
end
if true
% code
end
Does somebody know what I'm doing wrong? Thanks in advance and regards,
Emma

採用された回答

Arthur
Arthur 2012 年 10 月 22 日
In your code, n is a string (you make it a string with num2str). In the if statement, you compare n with a number, so it will always be false... My guess is that you wanted to use str2num or str2double instead ;).
  1 件のコメント
Emmanuelle
Emmanuelle 2012 年 10 月 22 日
編集済み: Emmanuelle 2012 年 10 月 22 日
Thank you a lot Arthur, now it works with str2num! ;)

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by