Can I call Python from Matlab and generate C code for it?

611 views Asked by At

Let's consider this simple Matlab script:

p=py.list([1,2,3])
disp(py.len(p))

this prints 3 .

Is it possible to generate C code for this script?

1

There are 1 answers

4
Sam Roberts On BEST ANSWER

When you say "generate C code from this script", I assume you're asking whether it's possible to use MathWorks code generation products (e.g. MATLAB Coder) to convert this script to C code.

If so then no - MATLAB Coder converts MATLAB code to C, and it only converts a subset of the MATLAB language to C. Most of the MATLAB language is covered, but there are some notable exceptions, and calls out to Python are one of them. I'm not sure whether you're hoping that you might be able to convert the Python itself to C code, or whether you're just hoping to generate C code that would call out to Python - but neither are possible.

Of course, you could

  1. (manually) convert the Python code to MATLAB, and then use MATLAB Coder to generate C code from the MATLAB

or

  1. (manually) convert the Python code to C code, and have MATLAB call the C code.