Skip to content

Commit 346cdc8

Browse files
author
Mathieu Dubois
committed
DOC Updated doc/devel/matlab_interface_devel.rst: remove invalid caracters in example, PEP8-ize examples, language.
1 parent f5c206d commit 346cdc8

File tree

1 file changed

+10
-108
lines changed

1 file changed

+10
-108
lines changed

doc/devel/matlab_interface_devel.rst

Lines changed: 10 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -4,127 +4,29 @@
44
How to wrap a MATLAB script
55
===========================
66

7-
This is minimal script for wrapping MATLAB code. You should replace the MATLAB
8-
code template, and define approriate inputs and outputs.
9-
107

118
Example 1
129
+++++++++
1310

14-
.. testcode::
15-
16-
from nipype.interfaces.matlab import MatlabCommand
17-
from nipype.interfaces.base import TraitedSpec, BaseInterface, BaseInterfaceInputSpec, File
18-
import os
19-
from string import Template
20-
21-
class ConmapTxt2MatInputSpec(BaseInterfaceInputSpec):
22-
in_file = File(exists=True, mandatory=True)
23-
out_file = File('cmatrix.mat', usedefault=True)
24-
25-
class ConmapTxt2MatOutputSpec(TraitedSpec):
26-
out_file = File(exists=True)
27-
28-
class ConmapTxt2Mat(BaseInterface):
29-
input_spec = ConmapTxt2MatInputSpec
30-
output_spec = ConmapTxt2MatOutputSpec
31-
32-
def _run_interface(self, runtime):
33-
d = dict(in_file=self.inputs.in_file,
34-
out_file=self.inputs.out_file)
35-
#this is your MATLAB code template
36-
script = Template("""in_file = ‘$in_file';
37-
out_file = ‘$out_file';
38-
ConmapTxt2Mat(in_file, out_file);
39-
exit;
40-
""").substitute(d)
41-
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
50-
# a single line and stripped of any comments).
11+
This is a minimal script for wrapping MATLAB code. You should replace the MATLAB
12+
code template, and define approriate inputs and outputs.
5113

52-
mlab = MatlabCommand(script=script, mfile=True)
53-
result = mlab.run()
54-
return result.runtime
14+
.. literalinclude:: matlab_example1.py
5515

56-
def _list_outputs(self):
57-
outputs = self._outputs().get()
58-
outputs['out_file'] = os.path.abspath(self.inputs.out_file)
59-
return outputs
16+
.. admonition:: Example source code
6017

18+
You can download :download:`the source code of this example <matlab_example1.py>`.
6119

6220
Example 2
6321
+++++++++
6422

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()
94-
>>> hello.inputs.name = 'hello_world'
95-
>>> out = hello.run()
96-
>>> print out.outputs.matlab_output
97-
"""
98-
input_spec = HelloWorldInputSpec
99-
output_spec = HelloWorldOutputSpec
100-
101-
def _my_script(self):
102-
"""This is where you implement your script"""
103-
script = """
104-
disp('Hello %s Python')
105-
two = 1 + 1
106-
"""%(self.inputs.name)
107-
return script
108-
109-
110-
def run(self, **inputs):
111-
## inject your script
112-
self.inputs.script = self._my_script()
113-
results = super(MatlabCommand, self).run( **inputs)
114-
stdout = results.runtime.stdout
115-
# attach stdout to outputs to access matlab results
116-
results.outputs.matlab_output = stdout
117-
return results
118-
119-
120-
def _list_outputs(self):
121-
outputs = self._outputs().get()
122-
return outputs
123-
124-
125-
23+
By subclassing :class:`nipype.interfaces.matlab.MatlabCommand` for your main class, and :class:`nipype.interfaces.matlab.MatlabInputSpec` for your input spec, you gain access to some useful MATLAB hooks
12624

25+
.. literalinclude:: matlab_example2.py
12726

27+
.. admonition:: Example source code
12828

29+
You can download :download:`the source code of this example <matlab_example2.py>`.
12930

31+
.. include:: ../links_names.txt
13032

0 commit comments

Comments
 (0)