How to make binary file for each iteration in a loop?

6 ビュー (過去 30 日間)
sara shirafkan
sara shirafkan 2020 年 8 月 7 日
回答済み: Image Analyst 2020 年 8 月 7 日
dear all
I want to know how to create a binary file for each iteration in a loop. The following is a simple test code I made, but this code just creates the last file.
A=rand(10,10,10);
for k=1:2:7
c=k:k+3;
for j=1:2:7
b=j:j+3;
for i=1:2:7
a=i:i+3;
D=A(a,b,c)
fileID = fopen('ax.raw','w+')
fwrite(fileID,D,'uint8')
fclose(fileID)
end
end
end
  1 件のコメント
Rik
Rik 2020 年 8 月 7 日
Why are you using 'w+'? That looks like you want to append, but that is 'a'.

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

回答 (2 件)

Sudheer Bhimireddy
Sudheer Bhimireddy 2020 年 8 月 7 日
I think, instead of the code creating "only the last file", it is overwritting the same file, so you end up with only one file.
Try renaming the file based on the iteration number. Something like this:
for i = 1: n_iterations
filename = strcat('file_id_',num2str(i));
% open - write - close the file with this filename
end
Hope this helps.

Image Analyst
Image Analyst 2020 年 8 月 7 日
Try this:
baseFileName = sprintf('ax_%d_%d_%d.raw', k, j, i);
fullFileName = fullfile(folder, baseFileName); % folder can be pwd if you want the current folder.
fileID = fopen(fullFileName, 'wb'); % Write a binary file using 'wb'.

カテゴリ

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