Skip to main content

How to use Python in Matlab on Mac

Below is a step-by-step guide to enable the use of Python functions and commands in Matlab.


  • Check in Matlab command prompt if there is a version of Python connected with Matlab already by using ‘pyenv’ command. If it gives the information about the Python environment being used, then go to step 6, otherwise go to the next step.
  • Make sure that CPython is installed. It should be installed from Python.org to the recommended location and not in the form of some IDE like Anaconda.
  • Download the correct version by checking the compatibility with the Matlab version being used.
  • After it is installed, check if it works using the terminal.
  • Use ‘pyenv(Version=“path”)’ command in Matlab command window to tell Matlab what version of Python to use. Path is the location of the executable (exec) Python file that will be used by Matlab.
  • This should allow Python to be run in Matlab.
  • If some libraries need to be installed, they can be installed using the Terminal by using ‘pip3 install library-name’.
  • If a custom module needs to be used, Matlab needs to tell Python where that module is located. This can be done by the following command:

             if count(py.sys.path,’path-of-folder-inclosing-file’) == 0

                     insert(py.sys.path,int32(0),’path-of-folder-inclosing-file');

        end


        9. The methods from the module can then be read in Matlab as ‘py.moduleName.methodName()’, the built-in                 commands can be used simply like 'py.commandName'.


I enabled it on Mac which I use for work and haven't tried it on any other OS. If you know then please comment how to do so to guide others. I had to hassle to learn this process, hope this explanation saved you some time.

Comments

  1. Finally the knight in the shinning armour i've been looking for

    ReplyDelete

Post a Comment

Popular posts from this blog

                                                               Exploring Antalya After suffering from the usual end of semester burnout, it was time for the best remedy, travel. Having visited my high-school friends in Kayseri in the winters, we had to head somewhere else for this summer trip. Evaluating the travel costs and scrolling through the list of worth visiting cities, Antalya stood out and I decided to go for it with my friend Zunnur. The biggest factor in the decision were the flight tickets which we were able to get at a reasonably cheap price of 130 Turkish Liras (TL) per person for a return ticket on Pegasus Airlines. Usually the bus tickets to Antalya are more expensive and trains from Ankara don’t go there yet. On the night of 26 th  of may   we were ready to go although the flight was on the mo...