I'm running a matlab code within a shell script. This is how I do it,
matlab -nodesktop -nosplash -nojvm -r "my_program;quit"
This works fine. My matlab code prints out a single number, say "ans = 10" for example. I want to assign this to a variable in the shell script.
I tried doing this
cmd=$(matlab -nodesktop -nosplash -nojvm -r "nc;quit")
echo $cmd
But this is not what I want because when you run matlab from the terminal it prints out not only the variable ans but also extra stuff like matlab version number etc.
I suppose rather than making matlab print to screen I could write to a file and have the script read from the file but is there any other way to do this?
Thanks.
Can you post the output you are getting into $cmd variable and which part of it should be extracted?
Output from matlab onto screen
< M A T L A B (R) > Copyright 1984-2010 The MathWorks, Inc. Version 7.10.0.499 (R2010a) 64-bit (glnxa64) February 5, 2010 To get started, type one of these: helpwin, helpdesk, ordemo. For product information, visit www.mathworks.com. >> ans = 226
I need the to assign the last number on this line (226 for this case) to a variable X for example.
I tried the following
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | awk '{ print $NF }')`
But this gives
> Inc. (glnxa64) 2010 demo. www.mathworks.com. >> = 226
---------- Post updated at 06:25 PM ---------- Previous update was at 06:08 PM ----------
It appears that it's actually not a single line but multiple lines. I just the last field of the last line so I write
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | awk '{ field=$NF }; END{ print field}'`
But this doesn't seem to print anything
Try:
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | awk -vRS="\x0" '{ print $NF }'`
That also prints an empty line unfortunately.
And this?
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | perl -ln0e '/(\d+)\s+$/;print $1'`
1 Like
Try this
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | awk -F"[ =]" '{print $NF}'`
bartus11's both solution worked for me though!
--ahamed
---------- Post updated at 11:27 AM ---------- Previous update was at 11:24 AM ----------
Another one
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | sed 's/.*= \([0-9]*\)$/\1/g'`
--ahamed
---------- Post updated at 11:32 AM ---------- Previous update was at 11:27 AM ----------
One more
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | cut -d= -f2 | sed 's/ //g'`
--ahamed
This is really frustrating, none of the above methods seem to work.
I tried ahamed101's three suggestions with the following result
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | awk -F"[ =]" '{print $NF}'`
echo $cmd
screen. > Inc. (glnxa64) 2010 demo. www.mathworks.com. 226
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | sed 's/.*= \([0-9]*\)$/\1/g'`
echo $cmd
Warning: No display specified. You will not be able to display graphics on the screen. < M A T L A B (R) > Copyright 1984-2010 The MathWorks, Inc. Version 7.10.0.499 (R2010a) 64-bit (glnxa64) February 5, 2010 To get started, type one of these: helpwin, helpdesk, or demo. For product information, visit www.mathworks.com. >> ans = 226
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | cut -d= -f2 | sed 's/ //g'`
echo $cmd
Warning:Nodisplayspecified.Youwillnotbeabletodisplaygraphicsonthescreen. <MATLAB(R)> Copyright1984-2010TheMathWorks,Inc. Version7.10.0.499(R2010a)64-bit(glnxa64) February5,2010 Togetstarted,typeoneofthese:helpwin,helpdesk,ordemo. Forproductinformation,visitwww.mathworks.com. >> 226
bartus11's last line gives a blank line again
---------- Post updated at 10:03 PM ---------- Previous update was at 09:58 PM ----------
ahamed101, How did you check if it worked? was it using matlab?
Try this...
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | awk -F"[ =]" '/ans =/{print $NF}'`
Looks like it is on multiple lines
--ahamed
---------- Post updated at 09:32 PM ---------- Previous update was at 09:30 PM ----------
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | sed -n '/ans =/ s/ans = \([0-9]*\)$/\1/gp'`
--ahamed
1 Like
If the matlab output is on multiple lines, then I think you should separate the assignment from the splitting.
That way, the shell will have done its job of stripping off the newlines before you extract the last chunk.
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit"`
cmd=`echo $cmd | awk '{ print $NF }'`
echo $cmd
tyler_durden
1 Like
I've made some progress. tyler_durden's seems to work, but not with $NF. If I had $NF it still gives a blank line, but I manually tried different field numbers and found it was 43. So it now looks like,
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit"
cmd=`echo $cmd | awk '{print $43}'`
echo $cmd
226
This isn't obviously ideal as it may not always be 43. I guess the problem is I don't know exactly what the output from matlab is like.
Can you post output of:
matlab -nodesktop -nosplash -nojvm -r "nc;quit" | cat -eT
This is the output.
^[[?1h^[=$
< M A T L A B (R) >$
Copyright 1984-2010 The MathWorks, Inc.$
Version 7.10.0.499 (R2010a) 64-bit (glnxa64)$
February 5, 2010$
$
$
To get started, type one of these: helpwin, helpdesk, or demo.$
For product information, visit www.mathworks.com.$
$
>> $
ans =$
$
226$
$
Nice mess :). Try this:
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | cat -eT | perl -ln0e '/(\d+)\$\s+$/;print $1'`
Thanks for the help but that prints a blank line too :(.
What system are you using?
II'm using opensuse.
cat /etc/*elease
LSB_VERSION="core-2.0-noarch:core-3.2-noarch:core-2.0-x86_64:core-3.2-x86_64"
openSUSE 11.1 (x86_64)
VERSION = 11.1
And this?
cmd=`perl -ln0e '/(\d+)\s+$/;print $1' <(matlab -nodesktop -nosplash -nojvm -r "nc;quit")`
Same, it's a blank line. I think what I'll do is read the value to a file and re-read it in the script. Thanks for the help though.