0

I am trying to define a data structure in MATLAB that would contain data of different data types (similar to a structure in C) like so: uint8, uint32, uint8, uint32. What I then want to do is to serialise this data, send it to a Python script (e.g. through UDP) and deserialise them within the Python script. Could anyone suggest an easy way of doing that?

The way I have tried to implement that so far is by creating two different arrays, but I am not quite how to proceed from there:

A=uint8([])
B=uint32([])
A(1) = 2;
B(1) = 222222;
limp
  • 889
  • 2
  • 14
  • 22
  • 1
    You can use `typecast`: `C = [uint8(2), typecast(uint32(222222), 'uint8')];` – Rotem Oct 22 '19 at 15:05
  • 1
    I have never used it, but the _serialize_ function [here](https://www.mathworks.com/matlabcentral/fileexchange/34564-fast-serialize-deserialize) looks promising – Luis Mendo Oct 22 '19 at 15:08
  • In case both MATLAB and Python should run on the same system, network communication is not required: https://stackoverflow.com/a/29189167/2732801 – Daniel Oct 22 '19 at 21:00
  • @LuisMendo: Not sure how you would de-serialize this on the python side – Daniel Oct 22 '19 at 21:01
  • @Daniel You are right. I didn't think of that – Luis Mendo Oct 22 '19 at 21:13
  • Is it necessary that the types are preserved exactly at the Python end? – nekomatic Oct 23 '19 at 11:21
  • 1
    To be more specific: Python doesn't have native `uint8` or `uint32` datatypes, just `int`, so are you using something like numpy or ctypes which does support these types, or is it OK for both of these to arrive in Python as `int`s? – nekomatic Oct 23 '19 at 12:53

1 Answers1

1

In MATLAB you can use java classes. This opens up a lot of possibilities.

For example, you could use Google Protocol Buffers. Write a schema that defines your data structures. Compile that to both Java and Python source code. This source code will define classes representing your data structure, and those classes will have methods for serialising and deserialisting to the GPB wire format. Use the Java in MATLAB, and the Python in, well, Python.

Thus you can serialise data in MATLAB, deserialise it in Python, and all you've had to do is write a GPB schema.

For actually moving the data between MATLAB and Python, you could do worse than take a look at ZeroMQ. It's a whole lot easier to use that UDP or TCP sockets, and again is available in Java, Python, there's probably a MATLAB binding as well.

bazza
  • 7,580
  • 15
  • 22