djiao
djiao

Reputation: 463

run matlab and pass integer parameter from command line

I have a matlab function and I was able to run it from command line. Now I want to pass a parameter to the file from command line. The parameter is a integer. It seems when I pass from command line, it is always taken as a "char".

Here is how I run the command

matlab -nodesktop -nosplash -r "mycommand 3"

For example, if I have a function as

function [ ] = mycommand( a )
a = a+3;
disp(a)
end

it prints 54 instead of 6.

Is there a way to work around this? I don't want to check the type of the variable in my code.

Thanks

Upvotes: 0

Views: 3929

Answers (2)

Sam Roberts
Sam Roberts

Reputation: 24127

MarkD gave a good answer. Although you mentioned you might be unhappy doing this (I'm sure for good reasons), another option would be to put a little extra code in to the beginning of your function, which would convert character inputs to numerical if the command were called via matlab -r:

if ischar(a)
    a = str2num(a);
end

Upvotes: 1

MarkD
MarkD

Reputation: 4944

You need to execute the function as you would in the matlab interpreter:

matlab -nodesktop -nosplash -r "mycommand(3)"

(Notice the parenthesis around the 3)

Upvotes: 5

Related Questions