Cannot find anonymous function

조회 수: 4 (최근 30일)
Damien Watson
Damien Watson 2025년 11월 25일
댓글: dpb 2025년 11월 25일
I have an anonymous function that pulls out data from a table for use in an arrayfun. This looks something like (I have not been able to reproduce this error with a minimal example):
arrayfun(@(x)(any(myTable{i, "Column"}.(string(x)))), myArray)
This works fine most of the time, but occasionally, it will get to this line and throw the following error
Error using indexing
Cannot find function "@(x)(any(myTable{i, "Column"}.(string(x))))" in "MyClass.m".
Error in MyClass>@(x)(any(myTable{i, "Column"}.(string(x)))) (line ...)
arrayfun(@(x)(any(myTable{i, "Column"}.(string(x)))), myArray)
Once this error occurs, any future attempt to run this code fails with the same error. I have tried "clear all" and reloading the project. I noticed that I can run the code if I F9 it while at a breakpoint at the arrayfun line. The only workaround I have found is to restart MATLAB. This is not ideal though, as this error can occur quite a while into the code, wasting significant time. Is this a bug in MATLAB or am I doing something wrong/is there a better workaround? I am using R2022a.
  댓글 수: 4
Damien Watson
Damien Watson 2025년 11월 25일
편집: Damien Watson 2025년 11월 25일
My minimal example does not exhibit this behaviour, and all the anonymous functions in the rest of the codebase do not act like this, but here is the minimal example:
myTable = table(struct("a", {true; true; false}, "b", {false; true; false}), VariableNames="Column");
myArray = ["a", "b"];
for i = 1:height(myTable)
f = @(x)(any(myTable{i, "Column"}.(string(x))));
disp(arrayfun(f, myArray))
end
1 0 1 1 0 0
The error does not have any more details other than the unrelated stack.
I forgot to mention in my original post, but I did find this stack overflow thread on this issue from over a decade ago, with a broken link back to mathworks' website: How can matlab be unable to find an anonymous function? - Stack Overflow.
dpb
dpb 2025년 11월 25일
What if you passed the index as well as to the anonymous function? As is, it comes from the environment.
Code looks like it should work as is, but ...

댓글을 달려면 로그인하십시오.

채택된 답변

Damien Watson
Damien Watson 2025년 11월 25일
After looking at some other issues, I found a bug report (https://uk.mathworks.com/support/bugreports/details/2804253) that confirms this behaviour when using clear all/classes, which explains why I was unable to recreate this issue when I wasn't clearing. The suggested fix is to upgrade to 2023b+ or to use a workaround by using named functions instead of anonymous functions.
  댓글 수: 1
dpb
dpb 2025년 11월 25일
" confirms this behaviour when using clear all/classes, ..."
clear all or clear classes is almost never required; and will reduce code efficiency. See the clear doc <Tips> on advice for specific conditions.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by