Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 79d2025

Browse files
committed
unload the extension, while closing the Matlab process.
Also: error handling when no Matlab process is running.
1 parent 40f932b commit 79d2025

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

pymatbridge/matlab_magic.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from glob import glob
1818
from shutil import rmtree
1919
from getopt import getopt
20+
from urllib2 import URLError
2021

2122
import numpy as np
2223
try:
@@ -207,11 +208,20 @@ def matlab(self, line, cell=None, local_ns=None):
207208

208209
text_output = ''
209210
#imgfiles = []
211+
212+
try:
213+
if line_mode:
214+
e_s = "There was an error running the code:\n %s"%line
215+
result_dict = self.eval(line)
216+
else:
217+
e_s = "There was an error running the code:\n %s"%code
218+
result_dict = self.eval(code)
219+
except URLError:
220+
e_s += "\n-----------------------"
221+
e_s += "\nAre you sure Matlab is started?"
222+
raise RuntimeError(e_s)
223+
210224

211-
if line_mode:
212-
result_dict = self.eval(line)
213-
else:
214-
result_dict = self.eval(code)
215225

216226
text_output += result_dict['content']['stdout']
217227
# Figures get saved by matlab in reverse order...
@@ -252,7 +262,16 @@ def load_ipython_extension(ip, **kwargs):
252262
"""Load the extension in IPython."""
253263
global _loaded
254264
if not _loaded:
265+
global this_magics
255266
this_magics=MatlabMagics(ip, **kwargs)
256267
ip.register_magics(this_magics)
257268
_loaded = True
258269

270+
def unload_ipython_extension(ip, **kwargs):
271+
global _loaded
272+
if _loaded:
273+
global this_magics
274+
this_magics.Matlab.stop()
275+
276+
277+

0 commit comments

Comments
 (0)