saving files in a loop with different names

49 ビュー (過去 30 日間)
MiauMiau
MiauMiau 2015 年 9 月 9 日
回答済み: Image Analyst 2015 年 9 月 10 日
Hi
My code looks something like:
for k = 1:length(sixCenters)
grouped(:,:,k) = fisher_matrix;
filename = strcat(sixCenters(k),'.mat');
save(filename,grouped);
continue; end
end
fisher_matrix is just matrix of doubles, sixCenters is a 6x1 cell, containing charachters like "NYU", "Lond".. The way I construct filename, it is indicated as char, so the line where I want to save the matrix "grouped" throws the following error:
Error using save
Argument must contain a string.
Error in example_Script(line 22)
save(filename,grouped);
What can I do? Thanks

採用された回答

Image Analyst
Image Analyst 2015 年 9 月 10 日
Try this:
folder = pwd; % or whatever you want.
for k = 1 : length(sixCenters)
grouped(:, :, k) = fisher_matrix;
baseFileName = sprintf('%s.mat', sixCenters{k});
fullMatFileName = fullfile(folder, baseFileName);
save(fullMatFileName, 'grouped');
end

その他の回答 (1 件)

Hamoon
Hamoon 2015 年 9 月 9 日
you should pass the string of the name of your variable, use this:
save(filename,'grouped');
  2 件のコメント
MiauMiau
MiauMiau 2015 年 9 月 9 日
that does NOT solve the problem, same error message. the problem is as indicated with filename
Hamoon
Hamoon 2015 年 9 月 9 日
編集済み: Image Analyst 2015 年 9 月 10 日
oh, you've got problem with filename too. you should use braces instead of parentheses for a cell array to get its value, when you use parentheses you refer to a cell with one dimension. So rewrite that section of your code as:
filename = strcat(sixCenters{k},'.mat');
and also you need to do what I said before:
save(filename,'grouped');

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

カテゴリ

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