Matlab Calling Functions without parentheses

前端 未结 1 604
孤独总比滥情好
孤独总比滥情好 2020-12-06 08:08

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

Example:

clear xx


        
相关标签:
1条回答
  • 2020-12-06 08:43

    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.


    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.

    0 讨论(0)
提交回复
热议问题