Is rowfun Showing Strange Behavior when the Target Function Returns an Empty Output?
Mostrar comentarios más antiguos
tonmoy
ha marcado con alerta este/a pregunta
Define a function that echos a scalar input, unless the input is 3, which returns an empty row vector 1x0.
function y = fun1(x)
if x == 3
y = zeros(1,0);
else
y = x;
end
end
Create a table
T = table((1:5).','VariableNames',"x");
As expected, the size of T is 5 x 1 (though I don't understand the return of ans here; I don't see ans when running locally with R2024a)
size(T)
Now call @doc:rowfun to execute fun1() on each row of T.x. What might be the impact of fun1 returning an empty matrix for row 3?
foo = rowfun(@fun1,T,"InputVariables","x","OutputVariableNames","y")
Not sure what happened there. Why isn't there any output displayed? What actually is foo?
foo
Still no output displayed.
Apparently foo is 5x1 table, even though rowfun presumably only returned four rows (because of the empty)
whos foo
We can access the variable, but foo.y only has four rows
foo.y
while the size of foo is 5 x 1 (and why is ans being shown)?
size(foo)
Common use of rowfun is to append to the original table.
T = [T,rowfun(@fun1,T,"InputVariables","x","OutputVariableNames","y")]
T
Why wasn't there any output? And why isn't there an error somewhere?
T is now being shown as 5 x 2
size(T)
x doesn't change
T.x
But T.y only has four rows. How can two variables in the same table have different number of rows?
T.y
Just out of curiosity, the third row of T is
T(3,:)
We can't access the "end" of the table (and I believe we'd get the same error if tyring T(5,:) )
try
T(end,:)
catch ME
ME.message
end
The behavior of the call to rowfun is different on R2024a, where it throws an error (f there is an anonymous function that is functionally equivalent to fun1 here)
But that output showing foo as a 5x1 table before the error message seems strange. Shouldn't the error prevent that output? And if that output is there, shouldn't that output show what foo actually is? And the error message doesn't really make sense anyway. Prior to that line foo wasn't even defined, so if rowfun is returning a 4x1, it should not be a problem to assign that output to the previously-nonexistent LHS variable.
Anyway, continuing in 2026a ...
Change the function so that an input of 3 returns a 0x0 empty
function y = fun2(x)
if x == 3
y = [];
else
y = x;
end
end
T = table((1:5).','VariableNames',"x");
T = [T,rowfun(@fun2,T,"InputVariables","x","OutputVariableNames","y")]
Now rowfun fails, as probably should be the expected behavior, but the error message makes no sense. How could fun2 return an output with more than one row when applied to the third row of T when the output in that case should be 0x0?
Should rowfun always throw an error if the output doesn't have the same number of rows as the input, as seems to be the case in 2024a? Maybe not if rowfun is being used in isolation, but then there should definitely be an error when that output of rowfun is horizontally concatenated with the original table.
And why is there no output from lines that don't end with a semicolon?
Respuesta aceptada
Más respuestas (1)
Matt J
el 23 de Sept. de 2026 a las 1:52
How could fun2 return an output with more than one row when applied to the third row of T when the output in that case should be 0x0?
The relevant section of rowfun is,
n = size(b_data{igrp,jout},1);
if grouped
...
elseif any(n ~= 1) % ~grouped
error(message('MATLAB:table:rowfun:UngroupedRowSize',funName,ordinalString(igrp)));
end
Here, n=0 but the code assumes that n~=1 is the same as n>1.
1 comentario
Paul
el 23 de Sept. de 2026 a las 2:54
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
