Clear Filters
Clear Filters

Create and save a file in a for cycle

2 views (last 30 days)
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

Accepted Answer

Thorsten
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

More Answers (1)

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

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!