Issue with running .m file in nipype

I’m trying to run an .m file in nipype. These are the contents of my myfile.m

M = magic(3);
csvwrite('/home/data/M.csv',M)

In python I then do

import nipype.interfaces.matlab as Matlab
matlab = Matlab.MatlabCommand()
matlab.inputs.script = '/home/data/matlab/test.m'
res = matlab.run() 

but M.csv is not created… Does inputs.script only accept strings that contain matlab code? Isn’t there a way to pass test.m directly?

Never mind, I figured it out. However I have a different problem now. I have an m file that computes a certain value which I would like my node to capture. Here is some made-up code that illustrates the problem. How can I specify in my class that the contents of the res variable should be stored in the value output?

class TestInputSpec(BaseInterfaceInputSpec):
    out_file = File('test.csv', usedefault=True)

class TestOutputSpec(TraitedSpec):
    out_file = File(exists=True)
    value= traits.Float()
    
class Test(BaseInterface):
    input_spec = TestInputSpec
    output_spec = TestOutputSpec

    def _run_interface(self, runtime):
        d = dict(out_file=self.inputs.out_file)
 
        script = Template("""
                             res=2.3+1.3;
                             csvwrite($out_file,res)
                              """).substitute(d)

        mlab = MatlabCommand(script=script, mfile=True)
        result = mlab.run()
        
        return result.runtime

    def _list_outputs(self):
        outputs = self._outputs().get()
        outputs['out_file'] = os.path.abspath(self.inputs.out_file)
        ***outputs['value'] = ??? #how can I assign the res result from script above to the value output?***

test = Node(Test(), name='test')