6

Please do not mark this as a duplicate of how to call python and sklearn from matlab? as the question is in my opinion not really answered.
Since Matlab Release R2014b I think, it is possible to directly use python from matlab.
In short words, you only have to place py in front of the python call.
I my setup (after giving matlab the python path with the command pyversion('PATH_TO_PYTHON'), that is running fine. I can even use dask multiprocessing. Quite cool. For example, executing py.dask.distributed.Client results in

  Python Client with properties:

              asynchronous: 0
                   cluster: [1×1 py.distributed.deploy.local.LocalCluster]
         get_futures_error: [1×1 py.method]
                coroutines: [1×1 py.list]
            scheduler_file: [1×1 py.NoneType]
                      loop: [1×1 py.tornado.platform.select.SelectIOLoop]
    recreate_error_locally: [1×1 py.method]
                  refcount: [1×1 py.collections.defaultdict]
                extensions: [1×1 py.dict]
                 scheduler: [1×1 py.distributed.core.rpc]
                       rpc: [1×1 py.distributed.core.ConnectionPool]
                   futures: [1×1 py.dict]
            scheduler_comm: [1×1 py.distributed.batched.BatchedSend]
                    status: [1×7 py.str]
           connection_args: [1×1 py.dict]
                        id: [1×43 py.str]
                generation: [1×1 py.int]
                   io_loop: [1×1 py.tornado.platform.select.SelectIOLoop]
                  security: [1×1 py.distributed.security.Security]

    <Client: scheduler='tcp://127.0.0.1:59795' processes=4 cores=4>

Coming back to the question: I have sklearn installed and can use it from the referenced python Installation. It is working the same way as dask. But MATLAB R2017a is not able to find sklearn.
A similiar call to the given above py.sklearn.cluster.dbscan results in

Undefined variable "py" or class "py.sklearn.cluster.dbscan".

Is there any python expert being able to explain?

2
  • 1
    Cannot reproduce. MATLAB R2017a works perfectly fine with sklearn here. No additional setup needed, just had to set the pyversion correctly. Are you 100% sure that you are using the same python version in the console and in MATLAB? Can you try creating a new virtual environment with sklearn and use that in MATLAB? Commented Aug 30, 2017 at 6:41
  • @hbaderts: Thanks for testing. I run Windows and have exactly one python all over the Computer. I'm exactly sure running/testing the same pyhton Versions (Anaconda 3 64 Bit). Which Setup are you running? Commented Aug 30, 2017 at 7:15

3 Answers 3

6

I got a solution from the mathworks Support.
It reads the way, that maybe the python environment is not completely setup. I was asked to start matlab from within the Anaconda Prompt which has that complete arranged environment. Running matlab from there yielded the wanted results thus being able to use for example sklearn.
Further comparing the diffenrences from there showed up, that some more directories from python have to be added to the systems search path.

Further I learned, that running py.importlib.import_module(<MODULENAME>) will show details if that python module and its dependencies are available or not.

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

Comments

2

On a Mac:

  • Open a new terminal window;

  • type: which python (to find out where the default version of python is installed);

  • Restart MATLAB;

  • type: pyversion('/anaconda2/bin/python'), in the command line (obviously replace with your path).
  • You can now run all the libraries in your default python installation.

For example:

>>py.sys.version;

>>py.sklearn.cluster.dbscan

Comments

1

You can create a conda environment and use it from MATLAB as follows. Note that it you are debugging Python at the same time, and making changes to some_awesome_python_module, you have to reload it every time (code below starting with clear classes is how to do that):

py_root_useFromMATLAB = fileparts(C:\anaconda_3\envs\useFromMATLAB\_conda.exe);
ENV = getenv('PATH');
ENV = strsplit(ENV, ';');
items_to_add_to_path = {
    fullfile(py_root_useFromMATLAB, 'Library', 'mingw-w64', 'bin')
    fullfile(py_root_useFromMATLAB, 'Library', 'usr', 'bin')
    fullfile(py_root_useFromMATLAB, 'Library', 'bin')
    fullfile(py_root_useFromMATLAB, 'Scripts')
    };
ENV = [items_to_add_to_path(:); ENV(:)];
ENV = unique(ENV, 'stable');
ENV = strjoin(ENV, ';');
setenv('PATH', ENV);
clear classes
module_to_load = 'some_awesome_python_module';
python_module_to_use = py.importlib.import_module(module_to_load);
py.importlib.reload(python_module_to_use);

Now you can use it like:

output = py.some_awesome_python_module.some_awesome_python_function(input)

2 Comments

My answer to a similar question about conda from MATLAB: stackoverflow.com/a/63671567/12763497

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.