Output of matlab as a variable for shell script

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Output of matlab as a variable for shell script
# 1  
Old 09-17-2011
Output of matlab as a variable for shell script

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?

# 2  
Old 09-17-2011
Can you post the output you are getting into $cmd variable and which part of it should be extracted?
# 3  
Old 09-17-2011
Output from matlab onto screen
 < M A T L A B (R) > Copyright 1984-2010 The MathWorks, Inc. Version (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
# 4  
Old 09-17-2011
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit"  | awk -vRS="\x0" '{ print $NF }'`

# 5  
Old 09-17-2011
That also prints an empty line unfortunately.
# 6  
Old 09-17-2011
And this?
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit"  | perl -ln0e '/(\d+)\s+$/;print $1'`

This User Gave Thanks to bartus11 For This Post:
# 7  
Old 09-17-2011
Try this
cmd=`matlab -nodesktop -nosplash -nojvm -r "nc;quit" | awk -F"[ =]" '{print $NF}'`

bartus11's both solution worked for me though!


---------- 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'`


---------- 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'`

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to use printf to output a shell variable path?

So I created two shell variables: COLUMN1_HEADING, COLUMN2_HEADING. They have values: COLUMN1_HEADING="John" COLUMN2_HEADING="123456789" How would I use printf to get it to print an output like this: $COLUMN1_HEADING\t$COLUMN2_HEADING\nJohn\t123456789\n Thanks! (3 Replies)
Discussion started by: steezuschrist96
3 Replies

2. UNIX for Dummies Questions & Answers

Shell equivalent of matlab fwrite function

I have some matlab code that sends the serial port elements of an array using matlab's fwrite function, e.g.: fwrite(s, , 'uchar'); What would be a unix shell equivalent? E.g., after successfully accessing the port using instructions here: Simple terminal serial port program for Linux/MacOSX... (3 Replies)
Discussion started by: darwin_886
3 Replies

3. Shell Programming and Scripting

Sending sqlplus output to a shell variable

I am trying to import a sqlplus output into a shell variable but it doesnt seem to be working. set -x export DEPENDENT_CR_NO=`sqlplus -s /nolog <<EOF conn username/passwd set heading off select dependency from custom_patches where patch_name='PATCH.zip'; exit; EOF` echo $DEPENDENT_CR_NO ... (2 Replies)
Discussion started by: beginer314
2 Replies

4. Shell Programming and Scripting

Output to shell variable

head -n 1 ~/.fvwm/config | grep -c "some-string" It's the output from grep I need to put into a shell variable. Also - I appreciate tips for other commands giving the same output. (2 Replies)
Discussion started by: Swepter
2 Replies

5. Shell Programming and Scripting

Running matlab script from within bash script

Hi guys, I'm new to scripting and I'm wondering if I can run a matlab script from within a bash script. Basically, at the minute I have a bash script which creates a file "bottfile.xyz" I then import "bottfile.xyz" into matlab using importdata('bottfile.xyz'). Then I just run the matlab... (0 Replies)
Discussion started by: jhunter87
0 Replies

6. Homework & Coursework Questions

Matlab help! Reading in a file with a variable filename

1. The problem statement, all variables and given/known data: I want to read in a file, and plot the data in matlab. However, I do not like hardwiring filenames into my codes, so I always give the user the option to specify what the filename is. I am pretty inexperienced with matlab, so I have no... (0 Replies)
Discussion started by: ds7202
0 Replies

7. Shell Programming and Scripting

Capturing output of procedure in variable in shell script

Hi guys I am calling one DB2 stored proc through unix. It is giving me below output. I want to capture the value 150 in one UNIX variable in shell script. Please let me know how I can achieve this. Thanks in advance Value of output parameters -------------------------- Parameter Name :... (5 Replies)
Discussion started by: vnimavat
5 Replies

8. Shell Programming and Scripting

How to give a variable output name in a shell script inside a for loop

Hi all I run my program prog.c in the following way : $ ./prog 1 > output.txt where 1 is a user defined initial value used by the program. But now I want to run it for many a thousand initial values, 1-1000, and store all the outputs in different files. Like $ ./prog 1... (1 Reply)
Discussion started by: alice06
1 Replies

9. Shell Programming and Scripting

sql output to shell variable

Hi I tried searching but I used to see , this has been answered may times. But I could not find. I am looking a SQLPLUS output to be assigned to shell variable. This is what I coded. ##/bin/ksh sqlplus dmishr/mishra#09@ps004d.world <<ENDOFSQL spool abc.log SET ECHO OFF NEWP... (4 Replies)
Discussion started by: dgmm
4 Replies

10. Shell Programming and Scripting

how to assign sql output data to shell script variable

Hi Guys ! I am new to unix and want to find out how we can make sql statement data to shell script variable? Any help/suggestion is greatly appreciated -Chandra (1 Reply)
Discussion started by: kattics
1 Replies
Login or Register to Ask a Question

Featured Tech Videos