4

I'm working on a medical robotics project that captures a series of images and then does some processing on them in MATLAB. Since a number of other things have to be done outside MATLAB, I'm using another language for the overall control, and using console commands to trigger other portions.

I have a single .m file with a single function that takes the filepath to the directory the images are in and does all the MATLAB processing. How can I call this from the command line? I've seen matlab -r "function(input)" discussed in some other answers here, but it doesn't work for me (I get a syntax error at the open paren). More specifically, I get: matlab: eval: line 1690: syntax error near unexpected token '('.

I've seen a few people saying this has to be done by calling a shell script (which I have no idea how to write), but a number of other people saying it's doable without that, can anyone clarify?

Additionally, assuming I've merely botched the matlab -r syntax, how does MATLAB know where to find the .m file? Does it need to be in whatever directory I'm running the command from?

1 Answer 1

4

I am guessing you are trying:

matlab -r test('hi')

and getting...

bash: syntax error near unexpected token `('

or something similar?

Answer: You need to use " " around the function(input), ie:

matlab -r "test('hi')"

This runs test.m in my current directory with the input string 'hi'.

To do this in a shell script named runmatlabcommand.sh, in say bash, you can just open a file and write:

#!/bin/bash
matlab -r "test('hi')"

and then execute this script from the command line by typing ./runmatlabcommand.sh. Make sure the script has execute permissions before you try to run it ;)

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

6 Comments

Ah, typo in the question actually. I was doing matlab -r "function(input)", with the quotes as you describe. Do I need the apostrophes around the input?
@Oblivious Sage Only if input is a string.
@Oblivious Sage Are you sure your script is in your current directory?
Yes, I'm just testing it using Terminal, and I explicitly moved to the directory the .m script is in before calling the command. I added the apostrophes around the input string but I'm still getting the same error at the open paren. I added the exact error message to the question.
@Oblivious Sage Ah! That is a syntax error coming from badly formatted code within your MATLAB script. Do you get the same error when you run the script from within the MATLAB terminal.
|

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.