How do I convert this matrix to a binary file?
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
-10 -10 -10 -34 -94 -229 -982 -1196 -1588 -1916 -2060 -2197 -2391
-10 -10 -10 -31 -93 -241 -1058 -1484 -1696 -1993 -2195 -2392 -2405
30 -10 -10 -39 -103 -252 -1013 -1566 -1982 -2129 -2389 -2467 -2598
30 -10 -10 -41 -219 -262 -1027 -1965 -2087 -2381 -2475 -2561 -2609
30 -10 -10 -45 -226 -661 -1040 -2038 -2188 -2392 -2537 -2607 -2800
30 30 -51 -51 -235 -878 -1502 -2169 -2378 -2400 -2599 -2736 -2989
30 30 -32 -32 -173 -1015 -2023 -2179 -2386 -2503 -2606 -2803 -2997
30 30 -69 -69 -256 -1026 -2063 -2274 -2394 -2541 -2613 -2900 -3055
30 30 -79 -78 -266 -1036 -2071 -2319 -2401 -2601 -2800 -2997 -3109
30 -13 -12 -86 -201 -681 -2075 -2380 -2587 -2608 -2807 -3003 -3151
30 -25 -24 -56 -129 -681 -1805 -2382 -2591 -2795 -2994 -3093 -3209
30 -33 -33 -40 -124 -314 -1692 -2379 -2653 -2800 -3001 -3196 -3287
40 -11 -11 -37 -115 -303 -1668 -2372 -2771 -2983 -3281 -3291 -3370
0 commentaires
Réponses (1)
Walter Roberson
le 31 Jan 2011
fid = fopen('output.dat', 'w');
fwrite(fid,M);
fclose(fid);
If all of the values are in the range near what you demonstrate, then
fid = fopen('output.dat', 'w');
fwrite(fid, M, '=>int16');
fclose(fid);
should store them efficiently as 16 bit signed integers instead of the 64 bit floating point numbers of the first set of code.
Sometimes, though, when people say "binary file" what they want is a file of text characters, each of which is '0' or '1', showing the bits that encode the values. If that is what you want, then,
terminator = char(10);
if ispc(); terminator = char([13 10]); end
fopen('output.txt','wt');
fwrite(fid, [dec2bin(typecast(int16(M.'),'uint16'),16), ...
repmat(terminator,numel(M),1)].');
fclose(fid)
Note that I arranged the above code to write the binary out across the columns, so that the second line of binary text corresponds to the second element of the first row.
When you are reading or writing binary, you have to pay attention to whether the file stores the values "down the columns" (the order that values are stored in memory in Matlab) or "across the rows" (the order humans tend to read in.)
It also helps if you make the file self describing by writing out the dimensions before the data; better yet if you put the number of dimensions before the dimensions so you know how many dimensions are stored in the file.
5 commentaires
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!