Skip to content

Commit 10bf980

Browse files
committed
DOC: Added matlab_wrap example that allows for more control and access to MAtlabCommand inputs and outputs
1 parent cbf4bcc commit 10bf980

File tree

1 file changed

+87
-5
lines changed

1 file changed

+87
-5
lines changed

Diff for: doc/devel/matlab_interface_devel.rst

+87-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ How to wrap a MATLAB script
77
This is minimal script for wrapping MATLAB code. You should replace the MATLAB
88
code template, and define approriate inputs and outputs.
99

10+
11+
Example 1
12+
+++++++++
13+
1014
.. testcode::
1115

1216
from nipype.interfaces.matlab import MatlabCommand
@@ -35,17 +39,95 @@ code template, and define approriate inputs and outputs.
3539
exit;
3640
""").substitute(d)
3741

38-
# mfile = True will create an .m file with your script and executed. Alternatively
39-
# mfile can be set to False which will cause the matlab code to be passed
40-
# as a commandline argument to the matlab executable (without creating any files).
41-
# This, however, is less reliable and harder to debug (code will be reduced to
42+
# mfile = True will create an .m file with your script and executed.
43+
# Alternatively
44+
# mfile can be set to False which will cause the matlab code to be
45+
# passed
46+
# as a commandline argument to the matlab executable
47+
# (without creating any files).
48+
# This, however, is less reliable and harder to debug
49+
# (code will be reduced to
4250
# a single line and stripped of any comments).
51+
4352
mlab = MatlabCommand(script=script, mfile=True)
44-
result = mlab.run()
53+
result = mlab.run()
4554
return result.runtime
4655

4756
def _list_outputs(self):
4857
outputs = self._outputs().get()
4958
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
5059
return outputs
5160

61+
62+
Example 2
63+
+++++++++
64+
65+
By subclassing **MatlabCommand** for your main class, and **MatlabInputSpec** for your input and output spec, you gain access to some useful MATLAB hooks
66+
67+
.. testcode::
68+
69+
import os
70+
from nipype.interfaces.base import File, traits
71+
from nipype.interfaces.matlab import MatlabCommand, MatlabInputSpec
72+
73+
74+
class HelloWorldInputSpec( MatlabInputSpec):
75+
name = traits.Str( mandatory = True,
76+
desc = 'Name of person to say hello to')
77+
78+
class HelloWorldOutputSpec( MatlabInputSpec):
79+
matlab_output = traits.Str( )
80+
81+
class HelloWorld( MatlabCommand):
82+
""" Basic Hello World that displays Hello <name> in MATLAB
83+
84+
Returns
85+
-------
86+
87+
matlab_output : capture of matlab output which may be
88+
parsed by user to get computation results
89+
90+
Examples
91+
--------
92+
93+
>>> hello = HelloWorld(matlab_cmd = 'mymatlab')
94+
>>> hello.inputs.name = 'Monty'
95+
>>> hello.inputs.mfile = True #creates mfile
96+
>>> hello.inputs.paths = '/path/to/matlab/toolbox'
97+
>>> hello.inputs.script_file = 'helloworld_pyscript.m'
98+
>>> out = hello.run()
99+
>>> out.outputs['matlab_output']
100+
"""
101+
input_spec = HelloWorldInputSpec
102+
output_spec = HelloWorldOutputSpec
103+
104+
def _my_script(self):
105+
"""This is where you implement your script"""
106+
script = """
107+
disp('Hello %s Python')
108+
two = 1 + 1
109+
"""%(self.inputs.name)
110+
return script
111+
112+
113+
def run(self, **inputs):
114+
## inject your script
115+
self.inputs.script = self._my_script()
116+
results = super(MatlabCommand, self).run( **inputs)
117+
stdout = results.runtime.stdout
118+
# attach stdout to outputs to access matlab results
119+
results.outputs.matlab_output = stdout
120+
return results
121+
122+
123+
def _list_outputs(self):
124+
outputs = self._outputs().get()
125+
return outputs
126+
127+
128+
129+
130+
131+
132+
133+

0 commit comments

Comments
 (0)