Help me to store the values into a text file.
2 views (last 30 days)
Show older comments
Aiswarya Babu
on 11 Jun 2021
Commented: Walter Roberson
on 11 Jun 2021
These are the values i need to store as a text file .
29 C3 50 5F 57 14 20 F6 40 22 99 B3 1A 02 D7 3A
Kindly help me.
0 Comments
Accepted Answer
Walter Roberson
on 11 Jun 2021
S = '29 C3 50 5F 57 14 20 F6 40 22 99 B3 1A 02 D7 3A'
Schar = char(sscanf(S, '%x', [1 inf]))
[fid, msg] = fopen('a text file.txt', 'wt');
if fid < 0
error('failed to open file for writing because: "%s"', msg);
end
fprintf(fid, '%s\n', Schar);
fclose(fid);
You should not be astonished if you can only read back 12 out of the 16 bytes of the result. You are using MS Windows, and in MS Windows, when you store as a TEXT file, the 0x1A (decimal 26) is the End Of File character. If you happened to have bytes that were 0A then you should expect that when you write as a TEXT file that those bytes would be converted to CR/LF pairs (0C 0A)
Which is to say that storing in TEXT files is a mistake when what you have to store is BINARY data.
3 Comments
Walter Roberson
on 11 Jun 2021
S = '29 C3 50 5F 57 14 20 F6 40 22 99 B3 1A 02 D7 3A'
Sbyte = uint8(sscanf(S, '%x', [1 inf]));
[fid, msg] = fopen('a sound file.wav', 'w');
if fid < 0
error('failed to open file for writing because: "%s"', msg);
end
fwrite(fid, Sbyte, 'uint8');
fclose(fid);
This assumes that the stream of bytes holds the entire wav file, and not just the sound data information. For example if the original file contained track information, then that would have had to have been transmitted as well in order for the above method to work.
If what was transmitted was just the sound data, then you need to tell us how the sound was encoded into binary. Most wav files (but not all) use 16 bit unsigned integers internally, but most people who read wav files in MATLAB receive double precision values instead, with a special 'native' option being required to receive the unsigned integers.
More Answers (0)
See Also
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!