Return Matlab output using nipype

The objective is to submit some argument from Python through nipype to a Matlab script name get_print. The get_print has been set to be under Matlab search path.

However, Im not sure how to extend the example to received output from Matlab.

The Matlab script is

function opt=get_print(ipt_str)

opt = 1 + 1;
if_possible_return_multiple_output=3 % Suggestion to return multiple output is welcome
end

I have the impression, the Python script should be something like

def _my_script(self): 
    """This is where you implement your script"""
    script = """
    %s=get_print(%s)
    """ % (self.inputs.name, self.output.val)
    return script

However, the compiler return an error;

AttributeError: ‘HelloWorld’ object has no attribute ‘output’

May I know how solve this issue?

The complete Python code is

from nipype.interfaces.base import traits
from nipype.interfaces.base import TraitedSpec
from nipype.interfaces.matlab import MatlabCommand, MatlabInputSpec


class HelloWorldInputSpec(MatlabInputSpec):
    name = traits.Str(mandatory=True,
                      desc='Name of person to say hello to')


class HelloWorldOutputSpec(TraitedSpec):
    matlab_output = traits.Str()


class HelloWorld(MatlabCommand):

    input_spec = HelloWorldInputSpec
    output_spec = HelloWorldOutputSpec

    def _my_script(self): # similar to _bipolar_reference
        """This is where you implement your script"""
        script = """
        %s=get_print(%s)
        """ % (self.inputs.name, self.output.val)
        return script

    def run(self, **inputs):
        # Inject your script
        self.inputs.script = self._my_script()
        results = super(MatlabCommand, self).run(**inputs)
        stdout = results.runtime.stdout
        # Attach stdout to outputs to access matlab results
        results.outputs.matlab_output = stdout
        return results

    def _list_outputs(self):
        outputs = self._outputs().get()
        return outputs


hello = HelloWorld()
hello.inputs.name = 'hello_world'
out = hello.run()
output_matlab=out.outputs.matlab_output
1 Like