Using user defined structure as precision to fread

5 ビュー (過去 30 日間)
Akash
Akash 2011 年 8 月 23 日
コメント済み: gujax 2023 年 6 月 4 日
Above lists all the possible precision value. They are the MATLAB defined data types.
Say the binary file i am reading follows the following repetitive basic structure: floatValue, 4byte, float type doubleValue, 8byte, double type
I define a structure: myStructure = struct('floatValue', single(213.5883), 'doubleValue', 57605.061382);
Now i want to be able to read my binary file as follows: fid = fopen('myBinaryFile.ext'); A = fread(fid,1,*'myStructure'*,'s'); %Read one element data
Finally i can access the data as: A.floatValue or A(1).floatValue A.doubleValue or A(1).doubleValue
Concern is reading in the custom structure format. Above gives a invalid precision error. What way can we read our cutsom structures?

回答 (2 件)

Walter Roberson
Walter Roberson 2011 年 8 月 25 日
To read repetitive binary structures, memmapfile() is the appropriate routine.
  2 件のコメント
Akash
Akash 2012 年 3 月 5 日
Thanks that worked! :)
gujax
gujax 2023 年 6 月 4 日
I would like to follow up on this...
I am not sure if Matlab's fread is derived from C++'s fread?
C++'s fread or even istream can read an array of structured data (I just learned it from chatGPT). So why Matlab's fread is limited to a fixed type read?

サインインしてコメントする。


Kaustubha Govind
Kaustubha Govind 2011 年 8 月 25 日
There is no precision format to directly read into a structure. You need to read the file into arrays/matrices and then later compile them into a structure. Perhaps TEXTSCAN will be more appropriate for youm because it allows you to use a format string that is composed of several different types - the values are read into a cell array, which can store disparate types.
  1 件のコメント
Akash
Akash 2012 年 3 月 5 日
Thanks for your reply. Memmapfile solved my problem.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLow-Level File I/O についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by