1
votes

I have a 516096x1 vector with data samples that are all integer decimal values that looks like this but only the decimal column:

(DECIMAL)

1416
258
-258

2189

1545

I stored them into a variable. Now I want to write that variable to a binary file. The problem is, when I write the variable into a file, it replaces certain values incorrectly.

my code is:

Samples = (all the 516096 samples)
fwrite(fid1, Samples, 'int16')

It will write all the integers to the file in hex (using hex editor) but whenever it reaches the decimal integer that's equivalent to 8D it replaces it with 3F in the hex editor. 8F gets changed to 3F and 81 gets changed to 3F. also 0A gets replaced with 0D. why does Matlab do that. I've read it in as int16 and wrote it as int16.

2
How can it be a 1x1 column vector with 516096 elements? Maybe you mean "a 516096x1 vector" - tashuhka
yes thats correct. that's what i meant to say thank lol - Stokes
Can you include the code where you open the file to write (fid1) and where you do the hex conversion? - tashuhka
fid1 = fopen('I_New.bin','w+'); Here's what I did. I had Matlab read in data and place the data in a 1x516096 vector. When it brought the data in, It replaced the data with integers (negative and pos). Basically im tryin to take the same data and write it to a created binary file. But when i do that It replaces some data wrong but not all. It was read in as int 16 and im trying to write as int 16 but it replaces only those certain values i mentioned - Stokes
when i write the samples to the created file it spits out all the text characters but when this created file is dragged over to the hex editor app, thats when im able to notice those specific values i mentioned changes. Hex editor displays all of the integer values that were written to the file in hex little endian. so for ex: 1416 (integer) is 0588 (little endian) or 8805 (big endian). im using little endian. so when it gets down to 2189 (integer) it replaces it with 2111 (integer) so when put in hex editor what was suppose to be 2189 (088D) becomes 2111 (083F). PLEASE TELL ME WHY? - Stokes

2 Answers

0
votes

You are using signed ints (as pointed by tashuhka) and apparently, 16 bits are not enough for you - you have overflows.

Since you do need signed numbers (you have negative numbers as well) you should use 32 bits:

fwrite( fid1, Samples, 'int32');
0
votes

Ok. I've figured it out. I was using Matlab's text editor file in hex editor which didn't recognize those certain integer values BECAUSE they aren't recognizable calues in matlabs text editor. so Matlab will replace the unknown integers with its own i.e 3F replaced the original 8D. I then saved the file that matlab created to the desktop and dragged it over to hex editor which shows the replaced values. it shows them because your viewing the saved matlab text editor file instead of the actual raw data file. that files gets placed in the same directory as your code, script, functions, etc... Once you've saved your created file to a directory make sure to use that file from the directory.