save output values in for loop to text file

2 ビュー (過去 30 日間)
Turbulence Analysis
Turbulence Analysis 2020 年 8 月 19 日
コメント済み: KSSV 2020 年 8 月 19 日
Hi,
I have a set of operations which is performed inside the for loop. I intend to save result of each iteration into the single text file.
Actually I have set of 10 files B00001 to B00010, while reading each file ''output" geneates 1 X 4 values. I need to save this in a single text file such that 10 X 4, after fisnishing the oeration with all 10 files...
I tried with below code, but this saves only the result pertains to last file.. Somebody please help me with this....
for f = 1:1:10
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
fname_strt='B000';
elseif (f>=100) && (f<=999)
fname_strt='B00';
else
fname_strt='B0';
end
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.txt');
A=dlmread(fname,'\t',1,0);
sz = [112 98];
x = reshape(A (:,1),sz);
y = reshape(A (:,2),sz);
u = reshape(A (:,3),sz)';
v = reshape(A (:,4),sz)';
x1 = x(:,1);
y1 = y (1,:)';
output=IDvortex(x1,y1,u,v);
end
M=output;
dlmwrite('local circulation.txt',M,'delimiter','\t','precision',3)

回答 (1 件)

KSSV
KSSV 2020 年 8 月 19 日
編集済み: KSSV 2020 年 8 月 19 日
for f = 1:1:10
if (f>=1) && (f<=9)
fname_strt = 'B0000' ;
elseif (f>=10) && (f<=99)
fname_strt='B000';
elseif (f>=100) && (f<=999)
fname_strt='B00';
else
fname_strt='B0';
end
fname_end = num2str(f);
fname = strcat(fname_strt,fname_end,'.txt');
A=dlmread(fname,'\t',1,0);
sz = [112 98];
x = reshape(A (:,1),sz);
y = reshape(A (:,2),sz);
u = reshape(A (:,3),sz)';
v = reshape(A (:,4),sz)';
x1 = x(:,1);
y1 = y (1,:)';
output=IDvortex(x1,y1,u,v);
M=output;
dlmwrite('local circulation.txt',M,'delimiter','\t','precision',3,'-append')
end
  2 件のコメント
Turbulence Analysis
Turbulence Analysis 2020 年 8 月 19 日
Hi,
Thanks for prompt response.. Actually this yields results saved in 10 seperate .txt files, but I intend to save in single .txt file..
KSSV
KSSV 2020 年 8 月 19 日
Edited the answer......if it is throwing any error....read the documentation of dlmwrite on how appending the data to exisitng file.

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

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by