create a filename with cell

3 ビュー (過去 30 日間)
Nicolas
Nicolas 2016 年 11 月 21 日
コメント済み: Image Analyst 2016 年 11 月 21 日
Hello, I would like to create different filenames inside of a loop using cell inputs and I was wondering what function I could use instead of sprintf.
Thank you.
my code looks like that:
count = 1;
IntFile = {'sr','sphi','sz','srphi','srz','sphiz'};
for i = 1 : numel(IntFile)
IntFileName = sprintf('%s_%d',IntFile(1),count-1);
end

採用された回答

Image Analyst
Image Analyst 2016 年 11 月 21 日
Your code is wrong - should be IntFile{i} using braces and i instead of IntFile(1) using parentheses and 1 - but anyway, what's wrong with using sprintf()? If you want you can use brackets and num2str(),
IntFileName = [IntFile{i}, '_', num2str(count-1)];
but why?
And use IntFileName{i} if you want to use IntFileName after the loop exits and want to have all of the strings still available.
  2 件のコメント
Nicolas
Nicolas 2016 年 11 月 21 日
braces instead of parentheses! I should have understood that! the 'IntFileName' will be use inside of the loop, but I didn't want to write everything down, just that little problem I had with the () instead of {} ! thanks!
Image Analyst
Image Analyst 2016 年 11 月 21 日
Nicolas, thanks for Accepting. You can read the FAQ http://matlab.wikia.com/wiki/FAQ#What_is_a_cell_array.3F for a good discussion of when to use parentheses or braces. Basically use braces when you want the contents of the cell and parentheses when you want to refer to the cell itself. Again, the FAQ may make this clearer.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by