Create and save a file in a for cycle

2 次查看(过去 30 天)
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

采纳的回答

Thorsten
Thorsten 2013-2-25
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

更多回答(1 个)

Javier Abanades
Javier Abanades 2013-2-25
You are right! I did it with
fid=fopen(char(['a',num2str(a),'.txt']),'a');
Thanks for your help!

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by