1

I have an array of C struct's like

typedef struct{
    uint32_t timestamp;
    uint16_t channels[4];
    float    value;
} sample_t;

Which is write to a file with something like

fwrite(&sample,sizeof(sample_t),1,fpLog)

What's a good way to load this into a array of struct in Octave/Matlab?

Edit: The goal is optimization for speed. The files are 1GB+ big and take a very long time to load in matlab. The files load 100x faster in numpy.

pkuhar
  • 581
  • 6
  • 17
  • you can do it with a mex function – user3528438 Oct 07 '16 at 22:12
  • I think you should upload an example of your generated file (perhaps 10 samples) and provide a link – Andy Oct 09 '16 at 07:06
  • If speed is the goal write an OCT file. It would take less than 30 lines of C++ sourcecode. And you have the benefit that you can define the struct in a header and include it from your exporting prog and from the reading OCT file so you can easily extent your struct. – Andy Oct 16 '16 at 17:44
  • This question is unclear: does the data loading need to work in **both** MATLAB and Octave, does it need to work in one of them, or are you just using Octave and *think* that it’s the same as MATLAB? – Cris Luengo Dec 18 '21 at 14:51

2 Answers2

1

There is aligned rules for each C/C++ struct/class. See for example Eric Postpischil's answer in How is the size of a C++ class determined?. Very imported citate:

For elementary types (int, double, et cetera), the alignment requirements are implementation dependent and are largely determined by the hardware.

So aligned rules for C/C++ compiler and Matlab/Octave can be different. You can solve yore problem:

  1. Write data to file by components:

    fwrite(&sample.timestamp,sizeof(sample.timestamp),1,fpLog)
    fwrite(sample.channels,sizeof(sample.channels[0]),sizeof(sample.channels)/sizof(sample.channels[0]),fpLog)
    fwrite(&sample.value,sizeof(sample.value),1,fpLog)
    
  2. Read file in Matlab/Octave by componets too:

    ts = fread(fplog,"uint32");
    sample.timestamp = ts;
    ch = fread(fplog,"uint16",4);
    sample.channels = ch;
    vl = fread(fplog,"float32");
    sample.value = vl;
    sample
    

Do not forget to open files in binary mode!!!

Community
  • 1
  • 1
SergV
  • 1,269
  • 8
  • 20
  • Hi, Good point on alignment in the struct. You could also mention endianes. with this one. Your suggestion works but it's slow. – pkuhar Oct 14 '16 at 18:26
0

A trick : one can generate the .m I/O statements by parsing the C-struct definition with the GDB Python API. When the binary file to read is produced by an ELF/object/executable and you have a Debug (gcc -g) version of it.