I am writting data from one file to another but not getting the expected data?

1 次查看(过去 30 天)
I have a data file called imu_data.bin, I am parsing that file and after parsing getting three outputs inertial_data,time_stamp and raw_data the size of inertial data is 24 bytes for one sample , time stamp is 4 bytes for one sample and for raw data is 28 bytes for one sample. I am trying to write the inertial data after parsing to another file for the purpose I am using the following code
My code is
filename = 'imu_data.bin';
[inertial_data,time_stamps,raw_data]=parse_imu_data(filename);
fid = fopen('test2.bin', 'w');
fwrite(fid, uint8(inertial_data), 'uint8'); % write data
fclose(fid);
As far as I concern after writting the code my output of the file test2.bin should be 24 bytes as I am writing only one sample but I am getting 8944 bytes. Let say it might be reading all the samples then the size of output file must contain nearly 35 thousand bytes as the total sample present in imu_data.bin is 1499.
I don't know why I am getting this pls help.
About my parsing file it is a pre existing file and the result after parsing are also correct and meeting the expected value.
  2 个评论
Walter Roberson
Walter Roberson 2015-11-30
Could you confirm that size(intertial_data) is 24 ?
Please also experiment with
fwrite(fid, inertial_data)
as the default is uint8
Nikhil Singh
Nikhil Singh 2015-11-30
Walter I am sure that size of inertial data for each sample is 24 bytes and I had already tried the solution give by u.

请先登录,再进行评论。

采纳的回答

Guillaume
Guillaume 2015-11-30
编辑:Guillaume 2015-11-30
Two things:
fwrite(fid, uint8(inertial_data), 'uint8')
This does not write just one sample, but all the samples.
More importantly, if your inertial_data is indeed 24 bytes (stored as what? uint32?), then teh above uint8() call will map any value above 255 to 255. You should either:
  • not convert the inertial_data. fwrite write it as bytes (uint8) anyway. The only thing you need to be wary off is endianness issues. You can specify the endianness in the call to fopen, or in fwrite.
fwrite(fid, inertial_data) %writes it as bytes anyway
  • convert to uint8 with typecast. This is only useful if you want to keep the data as uint8 for further processing as the above is simpler for just writing:
inertial_data_as_uint8 = typecast(inertial_data, 'uint8');
fwrite(fid, inertial_data_as_uint8);
To see the difference between uint8 and typecast, try the following:
uint8(uint32(512))
typecast(uint32(512), 'uint8')
  3 个评论
Guillaume
Guillaume 2015-11-30
A 32-bit integer is simply the juxtaposition of 4 bytes. When a computer writes a 32-bit number (in memory or in file) it simply writes these 4 bytes all at once. You can convert the 32-bit number into the individual 4 bytes, and write these one by one, but you are effectively doing the same thing.
The only thing you have to be careful about is endianness. Depending on the processor / OS, the 32-bit hexadecimal number 12345678 can be written in memory as the 8-bit array [12 34 56 78] (big endian) or [56 78 12 34] (little endian as used by x86/x64 processors)

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String Parsing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by