1

Hey so I have a bash command that echos a string based on reading some file. Say for simplicity it is like this

for line in `cat file`
do
if [ "$line" == "IwantThisLine" ]
then   
echo "True"
fi
done

And I have it saved as its own individual script. Its called readRef.sh. So now I want to call it in matlab and store whatever it outputs in a variable! I am not entirely sure on how to do that, I seem to get an error when using evalc() on a system(). But it could be just me messing up quotations.

I tried something like

evalc(system(['./readRef.sh ' bamfile']))

The "bamfile" is a variable that is just a string to the path of a bamfile.

I get this error.

>> tes = evalc(system(['./readRef.sh ' smplBamFile])) 
hg18 
??? Undefined function or method 'evalc' for input arguments of type 'double'.

Coincidentally it does spit out "hg18" which is what I want to set the matlab variable to be.

4
  • 1
    Well, could you provide the evalc/system commands you've tried to run the script and the accompanying error message? (As an aside - does the script itself work when you run it from bash (i.e. sh ./readRef.sh?) Have you set the executable bit?) Commented Mar 12, 2012 at 23:27
  • Looks like smplBamFile might not be a character string - could you do class(['./readRef.sh ' smplBamFile])? Commented Mar 12, 2012 at 23:34
  • I think that this specific file has no reference in the header, and since thats how my script reads references it is echoing nothing. So when I run it alone it returns 0. Do you think this is why? Commented Mar 12, 2012 at 23:42
  • if I type evalc(0) it gives me the exact same error. I'm fairly sure this is whats happening. Anyways lets pretend that its not giving me an error. Can you still help me with the original question? A simple example would do, or am I doing it right already? Commented Mar 12, 2012 at 23:43

2 Answers 2

3

Oh, I see. I don't think you need evalc at all. Reading the system docs you can just do:

[status, result] = system('echo True; echo "I got a loverly bunch of coconuts"')

And result will be

True
I got a loverly bunch of coconuts

So just do:

 [status, result] = system(['./readRef.sh ' smplBamFile])

The reason evalc isn't working is that it requires its input to be a Matlab expression in a string, but you are passing it the result of system.

You could try:

 evalc("system(['./readRef.sh ' smplBamFile])")

See how I'm passing in the system(...) as a string?

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I have already tried passing system(...) as a string but unfortunately that gave me an unexpected error. I will try what you said and let you know, thanks.
If you've tried passing it as a string too, then put that attempt and your error in the question. Whenever you ask a question, put all your attempts and the associated error messages in it - this will help greatly to people trying to help you, and prevent them from mis-identifying your problem as something it isn't.
0

The reason you get this error is because system(...) returns the return-code of the command it ran, not its output. To capture its output, use

[~, output] = system(...)
tes = evalc(output);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.