alternatives to putting multiple placeholders
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
I have an 1000000*8 matrix which I am trying to output to a text file.
fprintf(fid, '\n%12d %12d %12d %12d %12d %12d %12d %12d', P(:,1:8)')
If the matrix had more columns I would be writing the same placeholder (%12d) many more times. Is there a more efficient way to copy a large matrix to a text file preserving the rows and columns, possibly by writing the necessary placeholder just once.
0 Commenti
Risposte (2)
Walter Roberson
il 13 Mar 2012
No there is not.
You can, however, pre-calculate the format as a string and pass the string in to the call.
numcols = 8;
fmt = [ '\n', repmat('%12d ', 1, numcols) ];
fprintf(fid, fmt, P(:,1:8).');
0 Commenti
Jacob Halbrooks
il 13 Mar 2012
Since all of your data has the same format, you can let FPRINTF repeat the format specifier over all of your data. Here is an example of doing that:
data = rand(5,10);
for rInd = 1:size(data,1)
fprintf('\n');
fprintf('%12d ',data(rInd,:));
end
If your data is not regular enough for this, another approach would be to use REPMAT to dynamically create the format specifier for your data. I wrote an example of that here.
0 Commenti
Vedere anche
Categorie
Scopri di più su Whos in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!