Write a binary file with different value types

5 ビュー (過去 30 日間)
Anantha Padmanabhan Kidambi Sekar
Anantha Padmanabhan Kidambi Sekar 2017 年 11 月 14 日
回答済み: Jaswanth 2024 年 1 月 3 日
Hi,
I want to generate a binary file with 11 columns and where each column has different data types. Can you point out where I am going wrong in the code because my output is very confusing to read. The precision and bytes corrospond to each column of the data_matrix.
precision = {'uint16','int64','float32','float32','float32',...
'float32','float32','float32',...
'float32','float32','float32'};
bytes = [2 8 4 4 4 4 4 4 4 4 4];
data_matrix=[col1; col2; col3; col4; col5; col6; col7; col8; col9; col10; col11] ; %data to store in binary file
%writing to a binary file
fid = fopen('input.bin','w'); %with write access
for i=1:numel(precision)
fwrite(fid, lidar_data_cell(:,i), ['*' precision{i}], sum(bytes(1:i))); %fwrite (filename, data, precision, bytes to skip)
fseek(fid,bytes(i),'eof'); %fseek(file, bytes to skip, end of file)
end
fclose(fid);
I am not sure what goes on in the code but I cannot read back the data that I stored in the .bin file. I have checked the syntaxes for fwrite and fseek. Can anyone tell me what I am doing wrong in this?
Thanks,
Ananth

回答 (1 件)

Jaswanth
Jaswanth 2024 年 1 月 3 日
Hi,
As I understand you are able to write in the binary file but are not able to read the contents properly. After going through the code you provided, I have found that:
  • “data_matrix” is concatenating column vectors of potentially different data types and sizes into a single matrix, which MATLAB would not allow unless they are all the same type and have compatible dimensions. It should likely be a cell array if the columns have different types of data.
  • Also, Inside the loop, the “fwrite” function is called to write data to the file. The code snippet contains an error in the variable name “lidar_data_cell”, which is not defined in the provided snippet. It should be “data_matrix” or another variable that contains the actual data to write.
  • The “fseek” function is not needed as “fwrite” will automatically continue writing where the last write ended.
Please go through the following example code showing how you can write the binary file:
% Define the precision for each column
col1 = randi(255, numRows, 1, 'uint8'); % Unsigned 8-bit integer
col2 = randi([0,1], numRows, 1, 'logical'); % Logical ... You can define other column values similarly
% Replace with Code to Define the data matrix as a cell array
% Replace with Code to Open the binary file for writing
% Write data to the binary file
for row = 1:numel(data_matrix{1}) % Assuming col1 defines the number of rows
for i = 1:numel(precision)
% Handle string and complex data specially
if strcmp(precision{i}, 'char') && iscell(data_matrix{i})
% Write the length of the string followed by the actual string
str = data_matrix{i}{row};
strLength = length(str);
fwrite(fid, strLength, 'uint16');
fwrite(fid, str, 'char');
else
% Write the data as is for other types
fwrite(fid, data_matrix{i}(row), precision{i});
end
end
end
Please go through the following example code of how you can read the binary file created above:
% Read the data from the file until the end of the file is reached
while ~feof(fid)
% Read data column by column according to the specified data types
col1 = fread(fid, 1, 'uint8');
if isempty(col1); break; end % Exit loop if no more data
col2 = fread(fid, 1, 'ubit1'); % Similarly read the different column values.
% For strings, read the length first, then the actual string
strLength = fread(fid, 1, 'uint16');
col9 = fread(fid, strLength, 'char')';
end
Hope the above solution is helpful in resolving your issue.
Thanks.

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by