In [1]:
import os
import numpy as np
import pymatbridge as pymat
In [2]:
matlab = pymat.Matlab()
matlab.start()
Out[2]:
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]:
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]:
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)
In [6]:
matlab.is_connected()
Out[6]:
In [7]:
matlab.stop()
Out[7]:
In [9]:
matlab.is_connected()
Out[9]: