Using Function and rowfun

17 次查看(过去 30 天)
Hi,
I'm trying to run this function through every row of a table to perform a calculation to get an output angle of sequence component analysis in an electrical transmission circuit. I am using rowfun to try and output the function but i keep getting an error.
Dtab=rowfun(@ZAngle, SequenceTable, 'OutputVariableName', 'ZeroAngle');
function Azero = ZAngle(Vzr, Vzi)
if Vzr<0 && Vzi>0
Azero=180-((atan(abs(Vzi)/abs(Vzr)))*180/pi);
end
if Vzr>0 && Vzi<0
Azero=-(atan(abs(Vzi)/abs(Vzr))*180/pi);
end
if Vzr<0 && Vzi<0
Azero=-180+((atan(abs(Vzi)/abs(Vzr)))*180/pi);
end
if Vzr>0 && Vzi>0
Azero=(atan(abs(Vzi)/abs(Vzr)))*180/pi;
end
end
With 'Dtab' being the output table that i want, 'ZAngle' being the function name, the 'SequenceTable' being the table i am taking values from, and 'Vzi' and 'Vzr' being the column headings of values i want to input to the function from the table (These are real and imaginary parts of numbers in cartesian form) and Azero being the desired output.
There is 99000 rows of data in the table and i want to perform this calculation on each row of data.
I am getting this error:
Error using tabular/rowfun>dfltErrHandler (line 517)
Applying the function 'ZAngle' to the 1st row of A generated the following error:
Too many input arguments.
Error in tabular/rowfun>@(s,varargin)dfltErrHandler(grouped,funName,s,varargin{:}) (line 262)
errHandler = @(s,varargin) dfltErrHandler(grouped,funName,s,varargin{:});
Error in tabular/rowfun (line 282)
[b_data{igrp,:}] = errHandler(struct('identifier',ME.identifier, 'message',ME.message,
'index',igrp),inArgs{:});
Error in FinalMatrix (line 51)
Dtab=rowfun(@ZAngle, SequenceTable, 'OutputVariableName', 'ZeroAngle');
and when i put rowfun after the function definition i get:
Error: File: FinalMatrix.m Line: 70 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "ZAngle" function definition to before the first local function
definition.
Any help is appreciated (sorry about this being long),
Thanks!

采纳的回答

Julian
Julian 2021-2-16
You may need to set the 'InputVariables' parameter for rowfun to {'Vzr' 'Vzi' } to pass only these variables to your function. rowfun pays no attention to the names of your variables and will attempt to call the function with every variable in your table. That can only work when nargin(func) == width(tbl) when applying func to every row of tbl.
  1 个评论
Stephen Fleming
Stephen Fleming 2021-2-16
Thank you very much, that fixed it and is working now.
Much Appreciated!!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Tables 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by