MATLAB Answers

save output values in for loop to text file

21 views (last 30 days)
Turbulence Analysis
Turbulence Analysis on 19 Aug 2020
Commented: KSSV on 19 Aug 2020
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)

  0 Comments

Sign in to comment.

Answers (1)

KSSV
KSSV on 19 Aug 2020
Edited: KSSV on 19 Aug 2020
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 Comments

Turbulence Analysis
Turbulence Analysis on 19 Aug 2020
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 on 19 Aug 2020
Edited the answer......if it is throwing any error....read the documentation of dlmwrite on how appending the data to exisitng file.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!