#NEWLINE# @ magic_arguments ( ) #NEWLINE# @ argument ( '-i' , '--input' , action = 'append' , help = 'Names of input variable from shell.user_ns to be assigned to Matlab variables of the same names after calling self.pyconverter. Multiple names can be passed separated only by commas with no whitespace.' ) #NEWLINE# @ argument ( '-o' , '--output' , action = 'append' , help = 'Names of variables to be pushed from matlab to shell.user_ns after executing cell body and applying self.Matlab.get_variable(). Multiple names can be passed separated only by commas with no whitespace.' ) #NEWLINE# @ argument ( '-s' , '--silent' , action = 'store_true' , help = 'Do not display text output of MATLAB command' ) #NEWLINE# @ argument ( '-S' , '--size' , action = 'store' , default = '512,384' , help = 'Pixel size of plots, "width,height.' ) #NEWLINE# @ argument ( '-g' , '--gui' , action = 'store_true' , help = 'Show plots in a graphical user interface' ) #NEWLINE# @ argument ( 'code' , nargs = '*' ) #NEWLINE# @ needs_local_scope #NEWLINE# @ line_cell_magic #NEWLINE# def matlab( self , line , cell = None , local_ns = None ) : #NEWLINE# #INDENT# 'Execute code in matlab.' #NEWLINE# args = parse_argstring ( self . matlab , line ) #NEWLINE# code = ( line if ( cell is None ) else ( ' ' . join ( args . code ) + cell ) ) #NEWLINE# if ( local_ns is None ) : #NEWLINE# #INDENT# local_ns = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# #UNINDENT# ( width , height ) = args . size . split ( ',' ) #NEWLINE# self . Matlab . set_plot_settings ( width , height , ( not args . gui ) ) #NEWLINE# if args . input : #NEWLINE# #INDENT# for input in ',' . join ( args . input ) . split ( ',' ) : #NEWLINE# #INDENT# try : #NEWLINE# #INDENT# val = local_ns [ input ] #NEWLINE# #UNINDENT# except KeyError : #NEWLINE# #INDENT# val = self . shell . user_ns [ input ] #NEWLINE# #UNINDENT# self . set_matlab_var ( input , val ) #NEWLINE# #UNINDENT# #UNINDENT# try : #NEWLINE# #INDENT# result_dict = self . eval ( code ) #NEWLINE# #UNINDENT# except MatlabInterperterError : #NEWLINE# #INDENT# raise #NEWLINE# #UNINDENT# except : #NEWLINE# #INDENT# raise RuntimeError ( '\n' . join ( [ 'There was an error running the code:' , code , '-----------------------' , 'Are you sure Matlab is started?' ] ) ) #NEWLINE# #UNINDENT# text_output = result_dict [ 'content' ] [ 'stdout' ] #NEWLINE# imgfiles = result_dict [ 'content' ] [ 'figures' ] [ : : ( - 1 ) ] #NEWLINE# data_dir = result_dict [ 'content' ] [ 'datadir' ] #NEWLINE# display_data = [ ] #NEWLINE# if ( text_output and ( not args . silent ) ) : #NEWLINE# #INDENT# display_data . append ( ( 'MatlabMagic.matlab' , { #NEWLINE# #INDENT# 'text/plain' : text_output , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# for imgf in imgfiles : #NEWLINE# #INDENT# if len ( imgf ) : #NEWLINE# #INDENT# with open ( imgf , 'rb' ) as fid : #NEWLINE# #INDENT# image = fid . read ( ) #NEWLINE# #UNINDENT# display_data . append ( ( 'MatlabMagic.matlab' , { #NEWLINE# #INDENT# 'image/png' : image , #NEWLINE# #UNINDENT# } ) ) #NEWLINE# #UNINDENT# #UNINDENT# for disp_d in display_data : #NEWLINE# #INDENT# publish_display_data ( source = disp_d [ 0 ] , data = disp_d [ 1 ] ) #NEWLINE# #UNINDENT# if len ( data_dir ) : #NEWLINE# #INDENT# rmtree ( data_dir ) #NEWLINE# #UNINDENT# if args . output : #NEWLINE# #INDENT# for output in ',' . join ( args . output ) . split ( ',' ) : #NEWLINE# #INDENT# self . shell . push ( { #NEWLINE# #INDENT# output : self . Matlab . get_variable ( output ) , #NEWLINE# #UNINDENT# } )