0

I got the answer from the following question. It asks me to create a bash file. The question is in the title

How to call MATLAB functions from the Linux command line?

Thus I tried the following code, as given in the answer.

    b_exec=matlab
    X="localize(r,q)"
    echo ${X} > matlab_command_rq.m
    cat matlab_command_rq.m 
    ${matlab_exec} -nojvm -nodisplay -nosplash < matlab_command_rq.m
    rm matlab_command_rq.m

The original code in the answer was

    matlab_exec=matlab
    X="${1}(${2})"
    echo ${X} > matlab_command_${2}.m
    cat matlab_command_${2}.m
    ${matlab_exec} -nojvm -nodisplay -nosplash < matlab_command_${2}.m
    rm matlab_command_${2}.m

In the explanation, they mentioned that $1 was function and $2 was inputs. correspondingly, I replaced it with my function 'localize' and inputs (r,q)

But I got the following error

    localize(r,q)
    ./matlab_batcher.sh: 5: ./matlab_batcher.sh: -nojvm: not found

The echo seems to be working. But I really do not know what is happening after that. Could you please help me and tell me the right way to call the matlab function with its arguments???

I called it using the following statement

   sh ./matlab_batcher.sh localize r q
Community
  • 1
  • 1
Lakshmi Narayanan
  • 5,220
  • 13
  • 50
  • 92

1 Answers1

1

You need to change your b_exec back to matlab_exec, or you need to change

${matlab_exec} -nojvm ...

to

${b_exec} -nojvm ...

Either way, you need to make it consistent.

jjlin
  • 4,462
  • 1
  • 30
  • 23
  • could you please tell me wat you mean by saying I should make it consistent? And the way am calling the function, is it the correct way? – Lakshmi Narayanan Jan 25 '13 at 20:10
  • am getting the same error even after the change. :(. please help me here.! – Lakshmi Narayanan Jan 25 '13 at 20:11
  • 1
    I mean that you can't name your variable `b_exec` and then reference `matlab_exec` (which is then undefined). As for how you're calling it, since you hardcoded the function name and arguments into your script, you can just do `sh ./matlab_batcher.sh`. – jjlin Jan 25 '13 at 20:13
  • 1
    You're still getting `-nojvm: not found`? That seems unlikely. You should double-check your work. Failing that, post exactly what you now have. – jjlin Jan 25 '13 at 20:14
  • no, actually am getting this. ./matlab_batcher.sh: 5: ./matlab_batcher.sh: matlab: not found . Sorry for incomplete comment. – Lakshmi Narayanan Jan 25 '13 at 20:14
  • 1
    That means `matlab` isn't in your search path. The easiest thing to do is just use the full path to `matlab`, e.g. set `matlab_exec=/path/to/matlab` (wherever that is on your system). If you don't know, you'll have to ask your local support over there. – jjlin Jan 25 '13 at 20:16
  • Thanks a Ton. That worked. Another last doubt!! If I have to pass arguments from command line, what am I supposed to do? Instead of Hardcoding – Lakshmi Narayanan Jan 25 '13 at 20:25
  • 1
    Then you should go back to the original version of the code and make a call like `sh ./matlab_batcher.sh localize "r,q"`. – jjlin Jan 25 '13 at 20:27
  • this function localize(r,q) should be in the same directory of this bash file?? – Lakshmi Narayanan Jan 25 '13 at 20:31
  • It can be in any directory where MATLAB searches for functions, but the same directory would be one of those places. – jjlin Jan 25 '13 at 20:32
  • Wow. Looks like am learning a lot here, thanks to your patience. How do I know the directories where matlab searches for? Should I add them in search path, in matlab settings! – Lakshmi Narayanan Jan 25 '13 at 20:42
  • That's actually something you can easily search for on the web. – jjlin Jan 25 '13 at 20:51
  • ok ok. sorry for asking everything. I have another doubt. I will post that as a question. If you know, please help me. I will try searching in google as well. – Lakshmi Narayanan Jan 25 '13 at 20:53