Parsing variables from MATLAB to Linux and vice versa in shell

I am facing a problem. I need to use MATLAB with linux. I need to parse data from MATLAB to Linux and vice versa.

For example,

It's all written in

basic.sh

this basic.sh has to be opened in MATLAB

 s=3     # is defined is MATLAB
##########################


for (( p=1 ; p<5; p++ ))     # from here starts the loop in Linux
do                                # is a command from Linux
echo "$p"                         # is a command from Linux
add= $p+s                         # should calulate in linux , is a command from Linux
add=add/5                         # should do in MATLAB 
done     

#########################
 add                              # should OUTPUT the value of add as there is no semicolumn in MATLAB 

      

Please suggest me a possible way for such a small example, the rest I will expand it myself.

Regards

+1


source to share


1 answer


Well, you can call Matlab from terminal and run one command:

$ matlab -nodesktop -nojvm -nosplash -r <YOUR_COMMAND>

      

which <YOUR_COMMAND>

can be the function m-script /. The result of this can be redirected to shellscripts,

$ matlab -nodesktop -nojvm -nosplash -r <YOUR_COMMAND> | ./basic.sh

      

(your script should be able to handle pipes), or this whole command can be embedded in shell scripts,

#!/bin/bash

s=$(matlab -nodesktop -nojvm -nosplash -r <FUNCTION_GENERATING_S>)

<code generating $add>

result=$(matlab -nodesktop -nojvm -nosplash -r <SOME_FUNCTION($add)>)

      



Of course, you can also use files as memory. Matlab part:

s=3;      

fid = fopen('TMP.txt','w');
fprintf(fid, s);
fclose(fid);

!./basic.sh

fid = fopen('TMP.txt','r');
add = fscanf(fid, '%f');
fclose(fid);

      

Shell script:

#!/bin/bash

s=$(cat TMP.txt)
for (( p=1; p<5; p++ ))     
do                            
    echo "$p" 
    add=$(($p+$s))
    add=add/5                      
done

echo $add > TMP.txt

      

The advantage of this is that there is a strict separation between Matlab and shell script and only one m file is enough.

Of course, whichever option you choose - why would you do it in the first place? Matlab can do most of what bash can and is also platform independent (so if you switch to MS Windows it all works) ... so can you elaborate on this a bit?

+2


source







All Articles