Cannot find anonymous function
Mostrar comentarios más antiguos
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 comentarios
Mathieu NOE
el 25 de Nov. de 2025
hello
can you share a code + some data
Stephen23
el 25 de Nov. de 2025
To make debugging a little easier place the anonymous function on a new line:
fnh = @(x)(any(myTable{i, "Column"}.(string(x))));
arrayfun(fnh, myArray)
and give us the complete error message.
Damien Watson
el 25 de Nov. de 2025
Editada: Damien Watson
el 25 de Nov. de 2025
dpb
el 25 de Nov. de 2025
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 ...
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Matrix Indexing 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!