In [1]:
import os
import numpy as np
import pymatbridge as pymat

In [2]:
matlab = pymat.Matlab()
matlab.start()


Starting MATLAB on ZMQ socket ipc:///tmp/pymatbridge-051089d9-1f6e-4298-8228-e9e1a8b3f368
Send 'exit' command to kill the server
.......MATLAB started and connected!
Out[2]:
<pymatbridge.pymatbridge.Matlab at 0x7f4a10638080>

In [3]:
func_file = os.path.dirname(pymat.__file__) + '/matlab/www/demo_func.m'
res = matlab.run_func(func_file, {'a': 10})
res['result']


Out[3]:
11

In [4]:
res_dict = matlab.run_code('a=[1,3,7]')
res_dict = matlab.run_code('plot(a)')
res_dict = matlab.run_code('b=a+1')
matlab.get_variable('b')


Out[4]:
array([[ 2.,  4.,  8.]])

In [5]:
res_dict = matlab.run_code('figure; plot(b); b')

from IPython.core.displaypub import publish_display_data
imgfiles = res_dict['content']['figures']
text_output = res_dict['content']['stdout']

display_data = []
if text_output:
    display_data.append({'text/plain':text_output})
for imgf in imgfiles:
    try: # python 2
        image = open(imgf, 'rb').read().decode('utf-8') 
    except: # python 3
        image = open(imgf, 'rb').read()
        
    display_data.append({'image/png': image})

for d in display_data:
        publish_display_data(d)


b =

     2     4     8


In [6]:
matlab.is_connected()


.
Out[6]:
True

In [7]:
matlab.stop()


MATLAB closed
Out[7]:
True

In [9]:
matlab.is_connected()


Out[9]:
False