Calling a function with no arguments

45 次查看(过去 30 天)
Swati Sarangi
Swati Sarangi 2020-11-13
回答: Steven Lord 2024-8-6,14:59
Hi All,
In my program , I'm trying to call a function with some input arguments but no outputs as I'm using this function for the purpose of plotting.
How should its syntax look like?
[ ] = function_name( var1,var2,var3) // This line is giving me erorr : Assigning an output to an empty array is not supported.
Please suggest me a way to handle this error.
Thanks in advance!
  4 个评论
Torsten
Torsten 2024-8-6,14:33
So you searched for:
[a b c] = fun()
a = 1
b = 2
c = 3
function [a b c] = fun()
a = 1;
b = 2;
c = 3;
end
?
John Wolter
John Wolter 2024-8-6,14:53
More or less, yes. My issue is that I am trying to code in Matlab with a head full of Python knowledge. Simple questions like, "If I have no input arguments, do I need the parentheses after the function?" often have different answers in Matlab.
I'm going to edit my comment because technically a function output is considered an "output argument" in Matlab, another difference from Python. :-)

请先登录,再进行评论。

回答(2 个)

Steven Lord
Steven Lord 2024-8-6,14:59
Outputs but no inputs
To call a function with outputs but no inputs, you can omit the parentheses or just include the parentheses with nothing inside them. If you were calling a function handle with outputs but no inputs, you would need to include the parentheses with nothing inside them. Otherwise MATLAB thinks you're trying to assign that variable to one or more variables and that may not work.
P = pi % one output, zero inputs to the pi function
P = 3.1416
P = pi() % one output, zero inputs
P = 3.1416
f = @pi;
P = f() % This works
P = 3.1416
P = f % P is not the value of pi but the function handle itself
P = function_handle with value:
@pi
try % Need to try/catch so I can run more code later in this answer
f = @computer;
[computerType, maxArraySize] = f % This does not work
catch ME
fprintf("This call threw error '%s'.\n", ME.message)
end
This call threw error 'Insufficient number of outputs from right hand side of equal sign to satisfy assignment.'.
Inputs but no outputs
To call a function with inputs but no outputs, just don't specify the square brackets and the equals sign.
help('pi') % zero outputs, one input to the help function
PI 3.1415926535897.... PI = 4*atan(1) = imag(log(-1)) = 3.1415926535897.... Documentation for pi doc pi
Neither inputs nor outputs
To call a function with neither inputs nor outputs, just type its name. Or you could use the empty parentheses if necessary. Some functions may assign to ans while others will not.
clear all
whos % No variables in the workspace
pi % zero outputs, zero inputs, assigns to ans
ans = 3.1416
whos % The pi call added ans to the workspace
Name Size Bytes Class Attributes ans 1x1 8 double
clear ans % ans no longer exists
why() % zero outputs, zero inputs, displays but does not assign to ans
Penny suggested it.
whos % And nothing shows up

Mario Malic
Mario Malic 2020-11-13
编辑:Mario Malic 2020-11-13
You can achieve it this way.
function function_name( var1,var2,var3)
However, imagine a situation where you'd like to edit something on the plot that you've made, then you'd have to find the handles of it which is not a problem if you have only one figure/axes, but for multiples, you could return a figure handle.
function hFigure = function_name( var1,var2,var3)
hFigure = figure(1); % if you're using it
plot(hFigure, x, y);
end
  2 个评论
Stephen23
Stephen23 2020-11-13
@Mario Malic: are you sure about that function definition syntax? The syntax given in the documentation is:
function hFigure = function_name(var1,var2,var3)
Mario Malic
Mario Malic 2020-11-13
编辑:Mario Malic 2020-11-13
I haven't had my first coffee yet! I edited the answer, thanks Stephen.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Printing and Saving 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by