Resolution: No issue with Matlab, just my understanding of how to open / read the data. Apologies for the error.
This question is closed. Reopen it to edit or answer.
fwrite ascii output error
1 view (last 30 days)
When writing data to a file in binary format using fwrite(), in some instances I get the hex output and in some I get the ASCII output.
Example code to show the error is as below;
% data arrays
dataArr1 = [-1 1];
dataArr2 = [-1 -1];
% open file
fid1 = fopen('test1.dat', 'w');
fid2 = fopen('test2.dat', 'w');
% write data to file
fwrite(fid1, dataArr1, 'int16');
fwrite(fid2, dataArr2, 'int16');
% close file
In this case test1.dat shows the expected output of 'ffff 0100' whereas test2.dat results in 'ÿÿÿÿ' rather than the expected 'ffff ffff'.
Any ideas why it outputs hex in one instance but ASCII in the other?
Thanks in advance!
*EDIT - I'm checking the values by opening the files in basic editor (Sublime)
More Answers (1)
dpb on 21 Oct 2021
I see no such thing...
2×1 int16 column vector
You do have to read the file in a manner consistent with that in which it was written, however.
You did not show us how you came to the above conclusion...most likely given the symptoms by
>> type test2.dat
which treats a stream (binary) file as ASCII...you'll get something similar with the other file as well, of course, but the character CHAR(1) isn't displayable in the browser.
Just looking at the bytes written one sees precisely what one expects to have been written was