您的初始函数 twodcomp :您不能将输出变量(在 = 之前)命名为与函数名称相同(在 = 之后) .
然后,如果要使用 @ 表示法分配匿名函数(MATLAB docs,Octave docs),仍然可以传递所需的输入 .
所以重写它像:
% Include empty parentheses after a function name to make it clear which is the output
function output = twodcomp()
% Not sure why you're assigning this function to a struct, but
% still give yourself the ability to pass arguments.
% I'm assuming you want to use the output variable,
% and not reuse the main function name (again)
output.twoDperp = @(x,y) perp(x,y);
end
使用第二个函数,您只需要在输出参数之前删除 twoDperp . 在您的问题中,您说明了文档中的预期语法,但之后没有遵循它...
function [vmag, vangle] = perp(x,y)
W = hypot(y,x);
vmag = y/W;
vangle = x/y;
end
现在这些可以像这样使用:
% Deliberately using different variable names to make it clear where things
% overlap from the function output. twodcomp output is some struct.
myStruct = twodcomp();
% The output struct has the field "twoDperp" which is a function with 2 outputs
[m, a] = myStruct.twoDperp(1,2);