how to call matlab function in python script? - python

All my previous work (some big functions) are programmed by Matlab (some .m files). Now I get some robots of which the embedded functions are programmed by python. I want to apply my Matlab functions into the robots, but it will be very hard to recode the functions by python. I find two methods:
call matlab function in python script: it need the Matlab environment, but the robot is independent of my PC when it works. I can not install Matlab in the robot.
translate matlab to python. I installed numpy and scipy, and tried to configure the tool SMOP (under Win7), but there is always an import error: no module named readline.
Another possibility is to write an interface with C to call the .M function, and another interface of C called by python, and then link these two interfaces. But I have no idea with this work.
So I can not continue my work. Can you give me some advice? thank you.

Related

Combine Python script with different GUI

I have script in Python containing several functions, and one Callback (when event occurs).
I would like to release some "package" from my script , which could be importable and usable for other users.
Lets say: Some one can use it for creating GUI app in JAVA, another it will incorporate to his own C++ Gui app, and someone just will call my functions from another Python script.
Can you point me to right direction please?
Thanks.
So you want to be able to call Python functions from Python, Java and C++?
The Python case is just a matter of importing your module.
To call Python functions from Java, try Jython: https://www.jython.org/index.html
To call Python functions from C++, use the Python header files and this approach https://docs.python.org/3/extending/embedding.html. Alternatively, you can use Boost.Python: http://www.boost.org/doc/libs/release/libs/python/doc/index.html
Calling one language from another can be quite a tricky thing. An approach that would work in all languages and avoid major headaches would be to expose a proper public interface (command-line, http, etc) and consume this interface in the other languages.
Other references:
Calling Python in Java?
Calling python from a c++ program for distribution

Transfering Matlab scripts into python

I have a number of Matlab functions and scripts which have been developed over a couple of years. I want to continue using these scripts, but can't afford MatLab. That's why I would like to transfer them to python. Is there an easy way to do this? I am a beginner in Python and I have Spyder 3.7 installed on my computer.
Thank you!
You can try Octave - an open source free Matlab equivalent.
If you can run your functions and scripts in Octave, then you can call them from within Python using oct2py package in Python.
Look e.g. here.
By that, you wouldn't have to translate any code into another language (like Python), which would mean you could introduce new bugs which then you have to debug etc etc ...

Can I interact with a running Python session from MATLAB?

I have a Fortran library wrapped to Python (with f2py), and I want to call the library from MATLAB (2016b). Although it's possible to call the Fortran library from MATLAB using MEX files, to save some work I'm hoping it's possible to call it using the Python interface. I know that one can call Python code from MATLAB (e.g. py.calendar.isleap(2016)), but unfortunately it seems that this only gives access to standard Python libraries.
Since the library has initialization cost, I prefer to initialize once and maintain a running Python session, and write MATLAB wrappers for library initialization and the library routines I wish to call. I'm not entirely sure how the MATLAB and Python sessions can best communicate, but I guess writing and reading temporary text files is a viable option.
Any suggestions for a good way to maintain an active Python session in MATLAB, and pass input to Python and output back to MATLAB?

How to run an executable from Python?

I have an executable (face recognition) written by one of our programmers who's no longer with us. It takes a commandline argument and prints the result to STDOUT like this.
I need to be able to use this in Python but performance is important so I'm not sure if I should just use subprocess. I need to capture the output of the program so is it a better idea to modify the code to make it a C++ library instead and write a wrapper for it in Python? How would that impact performance compared to using subprocess?
I have looked into the Python docs for running C code and found this but the answer here is different. The second method does not even use Python.h so I'm a little confused. What's the best way to do what I need when performance is important?
At first I have to tell you I think subprocess method and running external application and reading output from STD output would be slow and unreliable. I won't recommend that method.
Instead, here is some suggestions:
You can create a Python extension in C or C++, you can find a lot of resources online about it. So you can convert your current code to Python extension format and just use Python extension skeleton template, integrate your functions and code in it and you'll have a python extension, simply you'll be able to call it's functions from Python code. See: http://docs.python.org/2/extending/index.html
Convert your current C++ code directly into a DLL and exports functions of your code you want to call from python if you are in Windows or simply convert it to a shared library if you are using Linux. It will be compiled to .so . Then you can call functions as an external library in your Python code. Example with explanation: http://www.linuxforu.com/2010/05/extending-python-via-shared-libraries/
I've never tried them, but it seems these projects can help you with what you want:
http://sourceforge.net/projects/pygccxml/?source=dlp
https://code.google.com/p/pybindgen/
http://www.cython.org/

Creating a shared library in MATLAB

A researcher has created a small simulation in MATLAB and we want to make it accessible to others. My plan is to take the simulation, clean up a few things and turn it into a set of functions. Then I plan to compile it into a C library and use SWIG to create a Python wrapper. At that point, I should be able to call the simulation from a small Django application. At least I hope so.
Do I have the right plan? Are there are any serious pitfalls that I'm not aware of at the moment?
One thing to remember is that the MATLAB compiler does not actually compile the MATLAB code into native machine instructions. It simply wraps it into a stand-alone executable or a library with its own runtime engine that runs it. You would be able to run your code without MATLAB installed, and you would be able to interface it with other languages, but it will still be interpreted MATLAB code, so there would be no speedup.
Matlab Coder, on the other hand, is the thing that can generate C code from Matlab. There are some limitations, though. Not all Matlab functions are supported for code generation, and there are things you cannot do, like change the type of a variable on the fly.
I remember that I was able to wrap a MATLAB simulation into a DLL file and then call it from a Delphi application. It worked really well.
I'd also try ctypes first.
Use the MATLAB compiler to compile the code into C.
Compile the C code into a DLL.
Use ctypes to load and call code from this DLL
The hardest step is probably 1, but if you already know MATLAB and have used the MATLAB compiler, you should not have serious problems with it.
Perhaps try ctypes instead of SWIG. If it has been included as a part of Python 2.5, then it must be good :-)

Categories