I have encountered a problem. I have to use MATLAB with linux.
I need to parse data from MATLAB to Linux and vice versa.
For exammple
This all is writtein 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
Kindly suggest me a possible way for such a small example the rest I will expand it myself.
best regards
Well, you can call Matlab from a terminal, and run a single command:
$ matlab -nodesktop -nojvm -nosplash -r <YOUR_COMMAND>
in which <YOUR_COMMAND>
can be an m-script/function. The output of this can be redirected into shellscripts,
$ matlab -nodesktop -nojvm -nosplash -r <YOUR_COMMAND> | ./basic.sh
(your script should be capable of handling pipes), or this entire 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)>)
You can of course also resort to using 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
Advantage of this is that there is a strict separation between Matlab and shell script, and only one m-file will suffice.
Of course, whichever way you choose -- why would you want to do this in the first place? Matlab can do most of what bash is capable of, and is also platform independent (so if you switch to MS Windows, it all still works)...so can you clarify this a bit?