How to save output of double loop?

1 回表示 (過去 30 日間)
Ammy
Ammy 2021 年 9 月 17 日
コメント済み: Ammy 2021 年 9 月 17 日
If I have
for i=1:n
....
for j=1:m
A=myfunction(...);
end
end
The resultant A's are images, I want to save all A.s in the folder
I have tried the following but it only store A only for j
for i=1:n
....
for j=1:m
A=myfunction(...);
end
imwrite(A,strcat('The folder where I want to store ',num2str{j},'.png'));
end
How to store the output A of double loop ?

採用された回答

Mohammad Sami
Mohammad Sami 2021 年 9 月 17 日
編集済み: Mohammad Sami 2021 年 9 月 17 日
You should use the fullfile function to create the filepath. Your call to imwrite function is in the outer for loop, hence it will only save once per outer loop. You should call it in the inner loop to save all. Also to avoid overwriting the files, you need both i and j values in the filename.
dirtosave = 'C:\mydir\';
for i=1:n
....
for j=1:m
A=myfunction(...);
fname = sprintf('A_i_%i_j_%i.png',i,j);
imwrite(A,fullfile(dirtosave,fname));
end
end
  1 件のコメント
Ammy
Ammy 2021 年 9 月 17 日
Thank you very much.

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

その他の回答 (1 件)

KSSV
KSSV 2021 年 9 月 17 日
A = zeros(n,m) ;
for i=1:n
for j=1:m
A=myfunction(...); % assuming output of the function is 1x1
end
end
imwrite(A,strcat('The folder where I want to store ',num2str{j},'.png'));

カテゴリ

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