How can I package / unpack Matlab workspace data into a binary file?

I want to pack some data from Matlab workspace into binary and then restore some data back to workspace after a while. (The data forms part of the header of the telemetry data log. It is useful to have the data that generated the telemetry directly in the file.)

I know that the team load

and save

will do just that, but I think my condition is unique. Once I save the workspace data to a file, I have another program (written in C) that opens the file and adds the telemetry data to the file. I do not think load

and save

cope with it.

I partially solved the problem by using a tool cstruct

found in the Mathworks File Exchange to package the data into a notepad and write it to a file using fwrite

. The caveat with this approach is that you cstruct

must know the packaging format when it is unpacked. The data format has changed and is likely to change, so it would be a good idea to remove this requirement.

Ideally, I would like to write the number of X bytes in the file header, followed by a binary blob (size X) containing the MAT format data, followed by the frame size of the telemetry data. When I want to unpack the data, I read X bytes of data, interpret it as a MAT file, and it magically appears in the workspace.

Is there a way to package workspace variables into a binary block and then unpack them without first knowing their structure , but the binary data is stored as part of a larger file ?

Edit: Added clarification that I don't think that load

and save

is what I need.

+3


source to share


1 answer


Matlab save command is exactly what you want. To load the variables again, use load .

Update: Now I can see what you are trying to do. My best idea is to create a .mat file with save

and then paste that file into my own binary (just copy the bytes). To recover Matlab data, extract those bytes to create a new .mat file, then use load

. This method is good because you are doing minimal binary formatting and handling any Matlab data.



Bonus: A good place for temporary .mat files is in tempdir .

+3


source







All Articles