Create and save a file in a for cycle

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일

0 개 추천

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일

0 개 추천

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

카테고리

도움말 센터File Exchange에서 File Operations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by