Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the return value from matlab in bash script?

If I have this matlab function

function [result] = matlab_test(param1, param2)

disp(sprintf('param1 : %s', param1));
disp(sprintf('param2 : %s', param2));

result = 'hello matlab';

And I want to call this matlab function in bash script like

matlab -nodesktop -nosplash -nodisplay -r "try, A=matlab_test('test','matlab'); end; quit"
echo $A

And I want this output

test
matlab
hello matlab

My requirement here is to find if it is possible to use the return value from Matlab in my bash script and also to pass it through the pipeline. I only want to make my application separated into small components (files) and communicate through pipeline/params. In short, I want to see how far I can wrap Matlab script in my bash script so that I can set my code architecture.
If I cannot do this, I'll simply bundle the part that I need the return value from matlab together into matlab script.

like image 619
Jessada Thutkawkorapin Avatar asked Apr 10 '12 08:04

Jessada Thutkawkorapin


1 Answers

You're asking two questions. I'll answer both, including why the second one might be impossible depending on your operating system (and definitely impossible on mine), then offer a suggestion on a problem dependent workaround.

First, I use a script like this when getting Matlab to interact with a shell.

#!/bin/sh 

cat <<EOF | matlab -nodesktop -nosplash -nodisplay 
A=matlab_test('$1','$2');
system(['export temp1=' A]); %doesn't work
setenv('temp2',A); %also doesn't work, I'll explain why below
exit
EOF
echo $temp1
echo $temp2

gives output:

[XXXXXX@compute-0-138 ~]$ ./stack_ex test matlab
Warning: No window system found.  Java option 'MWT' ignored

                            < M A T L A B (R) >
                  Copyright 1984-2010 The MathWorks, Inc.
                Version 7.12.0.635 (R2011a) 64-bit (glnxa64)
                               March 18, 2011


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.

>> param1 : test
param2 : matlab
>> >> >> 

So clearly the two versions of setting environment variables doesn't work. This leads us to your second question.

The reason behind the failure to 'echo' is that both system and setenv create shells that are closed when Matlab is closed. That is to say, Matlab cannot set environment variables outside the shell that called it.

There's a workaround for this for Windows systems discussed in this posting, that uses a tool from Microsoft. Also mentioned here.

I don't believe there's a workaround for *nix systems to set environment variables from within Matlab.

Here's a method to do something similar to what you described.

I'm assuming the use of echo is not what you actually want to do. Rather, I'm guessing that you'd like to use the string output stored in the environment variable to be used in further work with commands or scripts in the shell. One possible workaround would be the following:

#!/bin/sh 

cat <<EOF | matlab -nodesktop -nosplash -nodisplay 
A=matlab_test('$1','$2');
setenv('temp1',A); %doesn't work
[a b] = system(['echo ' '$' 'temp1'])
exit
EOF

Giving output: [XXXXXX@compute-0-138 ~]$ ./stack_ex_3 test matlab Warning: No window system found. Java option 'MWT' ignored

                            < M A T L A B (R) >
                  Copyright 1984-2010 The MathWorks, Inc.
                Version 7.12.0.635 (R2011a) 64-bit (glnxa64)
                               March 18, 2011


  To get started, type one of these: helpwin, helpdesk, or demo.
  For product information, visit www.mathworks.com.

>> param1 : test
param2 : matlab
>> >> 
a =

     0


b =

hello matlab

This shows that the command echo $temp1 was evaluated in the subshell created by setenv and temp1 holds the value assigned to it. The result of this call to the shell is now stored in b (a holds 0 indicating success). It is conceivable that you could pass the whole of what you would like to do in the shell through the system command, so that it runs in the subshell. We would have to know more specifics of your problem to give a complete assessment of this approach though.

Edits and followup **********************

The closest thing to wrapping Matlab and bash that I can think of is the following trick. You can pipe the output from the Matlab script to myresult.out with the following:

#!/bin/sh 

cat <<EOF | matlab -nodesktop -nosplash -nodisplay /> myresult.out 
A=matlab_test('$1','$2');
disp(['grepMe ' A])
exit
EOF

You can grep the grepMe line from myresult.out, pipe to sed, and select only the part of the output line you need, then pipe that on the the rest of your script. That's as close as I think you can get to what you're trying to do.

like image 128
Sevenless Avatar answered Nov 11 '22 01:11

Sevenless