0

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

2 Answers 2

5

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)

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

Comments

1

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

Comments

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.