save output values in for loop to text file
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
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 个评论
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 Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!