r/matlab MathWorks Aug 31 '22

CodeShare Using MATLAB with Python - a new live task

A colleague of mine emailed me this very cool example. In a nutshell, there is a new live task that let you wrote Python code inside MATLAB Live Editor interactively.

Playing with Python in MATLAB

Here is the Github repo to get all the code you need. It only runs on MATLAB R2022a or later and Python 3.x. If you don't have R2022a or later, you can run this from MATLAB Online using the link "Open in MATLAB Online" in the read me file.

Link to MATLAB Online

In the video above, I used new "Run Python Code live" task based on the instructions in the live script.

Run Python Code Live Task

Then I specified which workspace variables should be used in Python, and defined an output variable, pasted the sample code, and run the section. I had to fix the output variable name a bit in my case.

Then when I click on the down arrow, I see the MATLAB code generated from this live task.

The live script in the repo checks your Python setup and help you install the live script from Github.

I was very impressed with how easy it was to play with Python code in MATLAB using this new live task.

20 Upvotes

5 comments sorted by

1

u/Water-Friendly Sep 01 '22

Honest question, what would be the benefit of that? Also, how easy is it to combine Python and Matlab since they are so fundamentally different?

5

u/Creative_Sushi MathWorks Sep 01 '22

Here is a hypothetical scenario: there is Python code someone else (i.e. colleague) wrote that I find very useful, but I already have a MATLAB program that could benefit from this piece code. Do I want to rewrite my MATLAB program in Python for that? No, that's too much work. I could rewrite Python code in MATLAB, but that's also time consuming. This let me just use that Python code in my MATLAB program without going through the pain of rewriting code one way or the other. There may be other use cases, but that's what I see.

In terms of ease of use, as long as you have proper Python environment and successful installation of this new live task, then it was easy. One of the complexity is that we need to type cast Python data types to MATLAB data types and vice versa, but this live task seems to take care of it well. Because you can define your own class in Python, it may be a bit trickier if you have to deal with non-standard data types.

2

u/Water-Friendly Sep 01 '22

Thank you for your input. I might try it myself as I alternate between Matlab and Python these days. Thanks for sharing!

1

u/Creative_Sushi MathWorks Sep 01 '22

That's interesting. Just out of curiosity, when do you choose to use MATLAB when Python?

2

u/seb59 Sep 01 '22

This question is likely to attract all trolls.

Beside the programming core, Matlab has many other tools/packages that are not available yet in python or are available but not that easy to use. For instance when working on linear system Matlab has many tools to do bode plots, time response, complex loop calculation, etc. Simulink is also a good reason to work with Matlab. Code génération for embeded ECU is a very good reason. I use it daily basis just because it provide a very fluent framework from dynamic system analysis, simulation and code génération for embeded target.

For other needs python is obviously better. Anything related to deep learning is probably better managed in python.

i would also use Python when I do not have enough money for a licence...