How do I convert this matrix to a binary file?

-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

Risposte (1)

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 Commenti

@Walter: I'm surprised to see an answer to "forward me the matlab program,pls" here. I do not see any labour of the OP to solve his problem. So you are very polite.
BTW. DEC2BIN works well with DOUBLEs as long as they have integer values, so I'd omit the TYPECAST. And inside DEC2BIN all data are casted to DOUBLE again (at least in 2009a).
Didn't get for what the "M" in fwrite(fid,M); stands for......the result has an error showing
"??? Undefined function or variable 'M'.
Error in ==> binary2ascii22 at 2
fwrite(fid,M);"
I am an new user to matlab, do avoid silliness in my questions...
Thank you.
If you are absolutely new to Matlab, reading the Getting Started chapters in the documentations is a much better method to learn basics than posting in the MATLAB Answers list.
Sir Jack : U are absolutely right. I have been using this for a month and familiarizing it with time by learning the basics. Their was no problem in converting the other way (binary to ascii), but ascii to binary was a problem, thus asked as a query. If u have any answers...it is most welcomed.
M is you data matrix. And Walter's answer is exhaustive already.

Accedi per commentare.

Prodotti

Tag

Richiesto:

il 31 Gen 2011

Community Treasure Hunt

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

Start Hunting!

Translated by