How to write txt file contain double array and text header?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Danupon Subanapong
le 23 Mar 2019
Commenté : Danupon Subanapong
le 23 Mar 2019
Hello!!!!
I have to write a text file containg array and text header. My numeric array is in size of 33731x48. I knew that I can use fprint to output text file, but my double array has too many columns. I don't know how to set up the formatting operator for all double array element. I want all element to be floating point number (%f). I am thinking about writing 48 %f inside fprintf function For example,
fprintf(fid, '%f %f %f... %f r\n', array);
and there are 48 %f inside fprintf function, but it should be better way. So, could you please help me giving some advice?
2 commentaires
Réponse acceptée
Rik
le 23 Mar 2019
Modifié(e) : Rik
le 23 Mar 2019
You can use repmat or sprintf to construct the FormatSpec.
array=rand(3000,48);
fid=fopen('test.txt','wt');%opening with the t flag auto-converts \n to \r\n on Windows
fprintf(fid,'some cool header text\n');
FormatSpec=[repmat('%f ',1,size(array,2)) 'r\n'];%or should that have been \r\n instead?
fprintf(fid,FormatSpec, array);
fclose(fid);
3 commentaires
Rik
le 23 Mar 2019
Sorry, that was a typo. The repmat function call now creates a 2D array, instead of a vector, I'll edit my code to fix this.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!