how to convert matrix column into separate text?

Hi all, I have a 365*45 matrix, which represents one-year data for 45 subbasins. I would like to convert each column of the matrix to a separate text file (45 text file with a year data) and naming the file as subbasin_1, subbasin_2 etc. Any thought would be highly appreciated.

 Réponse acceptée

Rik
Rik le 20 Juin 2017
You obviously did not Google/Bing/Yahoo this.
You can use a loop, combined with sprintf to do this.
for n=1:45
filename=sprintf('subbasin_%d.txt',n);
fid=fopen(filename);
%
%put your writing code here
%
fclose(fid);
end

3 commentaires

Hydro
Hydro le 21 Juin 2017
Modifié(e) : Walter Roberson le 21 Juin 2017
Hi Rik,
Thanks, that get me going. here is my code with slight modification to yours.
for n=1:45
filename=sprintf('Precip_%d.txt',n);
fid=fopen(filename, 'wt');
fprintf(fid,'%.1f\n',my_Prec(:,n));
fclose(fid);
end
I would like to append these text file with a specific date (same across all the text file) on the top row of each text file i.e.
precip_1.txt
01011986
0.8
0.5
1.2
0.0
...
...
etc
After the fopen() and before the fprintf(), add another fprintf() that puts in the header you want.
Hydro
Hydro le 21 Juin 2017
Excellent. Thank you Walter and Rik.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Question posée :

le 20 Juin 2017

Commenté :

le 21 Juin 2017

Community Treasure Hunt

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

Start Hunting!

Translated by