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;
7
  • 1
    You can use typecast: C = [uint8(2), typecast(uint32(222222), 'uint8')]; Commented Oct 22, 2019 at 15:05
  • 1
    I have never used it, but the serialize function here looks promising Commented Oct 22, 2019 at 15:08
  • In case both MATLAB and Python should run on the same system, network communication is not required: stackoverflow.com/a/29189167/2732801 Commented Oct 22, 2019 at 21:00
  • @LuisMendo: Not sure how you would de-serialize this on the python side Commented Oct 22, 2019 at 21:01
  • 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 ints? Commented Oct 23, 2019 at 12:53

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.