Using Function and rowfun

28 views (last 30 days)
Stephen Fleming
Stephen Fleming on 16 Feb 2021
Commented: Stephen Fleming on 16 Feb 2021
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!

Accepted Answer

Julian
Julian on 16 Feb 2021
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 Comment
Stephen Fleming
Stephen Fleming on 16 Feb 2021
Thank you very much, that fixed it and is working now.
Much Appreciated!!

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!