简体   繁体   中英

How to save .mat file contents in memory in MATLAB?

I want to get the byte array of a .mat file of a specific matrix. How can I do that?

I can save a matrix on disk by this command:

save('a.mat', 'a')

Now, I do not want to save the .mat file on disk. Instead, I want it in memory to send it over a network.

You could use a couple of undocumented functions (used internally by save and load ) for serializing/deserializing data. This would allow you to encode your matrix a into a byte stream the same way save would when storing it in a .mat file:

byteStream = getByteStreamFromArray(a);

You can then send this byte stream data over a network and decode it on the receiving end like so:

a = getArrayFromByteStream(byteStream);

Well... if you want to send the binary content of a mat file through a network, you have no choice but proceeding as follows:

  1. Save your data to disk in mat format:

    save('a.mat','a');

  2. Read the saved mat file as a byte array:

    fid = fopen('a.mat','r'); data = fread(fid);

  3. Send the binary data through the network:

    send_data(data);

If you want to go for a much more elaborate approach (I don't recommend it by the way), like digging into a mat file in order to exctract data or building your own mat file at runtime, this is a good starting point .

I don't think this is possible. But you could write the MAT file to a temporary directory (see tempdir ) and then open it as a binary file, read its contents back in memory as a uint8 array or something like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM