Create and save a file in a for cycle
4 views (last 30 days)
Show older comments
Hi all,
My problem is a bit complex so I will try to explain as best I can.
I have data in 3 columns
Hs Tp Dir 2 3 200 3 2 100 2 3 320
I would like to create a new file with each row, so my first file it would be: a1.txt Hs=2 Tp=3 Dir=200
My second file a2.txt Hs=3 Tp=2 Dir=100
...
The problem is how I can do that...because if I want to use the function fprintf I would need to open the files before and the dmlwrite can not insert text in the matrix....
This is my code:
B=zeros; [m,n] = size(A);
for a = 1:1 fileout=strcat(CurrentDirectory,'a',a,'.txt'); Resultado=fopen('%What??');
Hm0= A(a,1);
fp= A(a,2);
mainang= A(a,3);
fprintf(Resultado,'Hm0= %1.3f \n fp= %1.3f \n mainang=%1.3f',Hm0,fp,mainang);
%MY SECOND OPTION
V={'Hmo=';'fp=';'mainang='};
Z={Hm0;fp;mainang};
B={V Z};
dmlwrite(fileout, B);
end
0 Comments
Accepted Answer
Thorsten
on 25 Feb 2013
data = [2 3 200
3 2 100
2 3 320];
for i = 1:size(data,1);
fid = fopen(['a' int2str(i) '.txt'], 'w');
Hm0 = data(i, 1);
fp = data(i, 2);
mainang = data(i, 3);
fprintf(fid, 'Hm0= %1.3f \n fp= %1.3f \n mainang=%1.3f', Hm0,fp,mainang);
fclose(fid);
end
0 Comments
More Answers (1)
See Also
Categories
Find more on File Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!