Reading Binary file of Matlab in C++

Hello everyone,

I wrote my code in Matlab and created a binary file.Now I am trying to transfer the code into C++ by using this binary file.However, I have no idea how to do it. I will be grateful if you please help me in this regard.
Thanks
all files are binary.

are you saying you created an executable using the compiler in matlab (if that still exists)? It used to make executables by generating C from the M scripts, but it was pretty awful when I used it years ago. If I recall, you have to use their proprietary library that hides the actual algorithms from you and their code linked to that. You should be able to get the source code generated, manipulated it from main into a function that you can call, tie in their library, and it should work with minor poking and prodding past that.

Or an output file of data?

Or something else?

Last edited on
Thanks for your reply.
Specifically speaking, I have created a binary in matlab as below:

fhd = fopen('Test.bin', 'w');
>> fwrite(fhd, ......, 'logical');
>> fclose(fhd);

( I didnt use source code generation)
Now I have to read this bunary in C++ and optimize my code since Matlab is a bit slow.
Its not difficult to read a file certainly, but you have to know how it is structured, of course. Is there a repeating record structure? If so, you could create a struct defining the layout of fields, i.e., some collection of integral, floating point, or textural data. After doing that you simply iteritively read in the records.
I think matlab will just write flat doubles back to back for this statement, assuming that what you wrote was just a vector or matrix of values.

Its been a while but the format as I recall was very simple for raw data. If you have complex or text or other non double data, it gets weird.

Open a smaller sample file in a hex editor and see what you get. Most good hexeds will tell you the int, float, double, string, etc value of the bytes as you mouse over. Should be able to unravel the format that way, which should again just be

aaaaaaaabbbbbbbb etc where aaaaaaa is the first double, b is the second, ... no filler values etc. There may be options in the matlab write command to get it into this simple format, but as I recall you can get it to this type of format.
Topic archived. No new replies allowed.