Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab Calling Functions without parentheses

What is the correct name for the situation where a Matlab-script calls a function, but provides arguments without parentheses?

Example:

clear xx

Alternatively, I could use parentheses and transfer a string with the variable name:

clear('xx')

How can I distinguish between both alternatives when googling for a solution?

Bonus Question: How can I put the content of a variable into a call that is NOT using parentheses? Specifically, a build-script using mcc with a dynamic -o filename option; calling mcc with parentheses would also be acceptable, but I don't know how to google that, hence this question.

Thank you!

like image 881
zuiqo Avatar asked Mar 09 '23 13:03

zuiqo


1 Answers

When you call a function without the brackets, it is called command syntax. Here are three links to relevant documentation:

  • syntax
  • command vs function syntax
  • scripts and functions

Bonus answer

You cannot use a variable when using command syntax. From the docs:

When calling a function using command syntax, MATLAB passes the arguments as character vectors.

So it would work like so:

abc = zeros(10); % Some matrix called abc
mystring = 'abc' % A string containing the variable name
% Option 1:
clear('abc')     % Clears the variable abc
% Option 2:
clear abc        % As per above docs quote, interpreted as clear('abc')
% Option 3:
clear mystring   % As per option 2, interpreted as clear('mystring') so doesn't work
% Option 4:
clear(mystring)  % Interpreted as clear('abc') so works as expected

When calling mcc as you suggest in the question, the tooltip shows you can in fact use function syntax, despite the documentation being entirely shown using command syntax.

tooltip


Notes

Using brackets is standard practise in MATLAB, since you also cannot get output values from a function when using command syntax.

Also from the 3rd docs link above, you can see a message discouraging the use of command syntax when using MATLAB.

Caution: While the unquoted command syntax is convenient, in some cases it can be used incorrectly without causing MATLAB to generate an error.

like image 71
Wolfie Avatar answered Mar 17 '23 11:03

Wolfie