I want to test a cell array of objects for objects of a certain class. I organize my files in packages and use import statements to make my code shorter.

The problem: cellfun(@(o) isa(o,'MyClassName'),myCellArray) seems to ignore the import declaration, returning false for cell array elements where invoking isa(myCellArray{i}, 'MyClassName') would yield true.

How can I make cellfun honor my import statements?

EDIT: Might the import statement not propagate to the anonymous function inside cellfun? If yes, how can I archieve this?

Minimal (Not) Working Example

Files:

myfun.m
+pkg/
+pkg/MyClass.m

myfun.m:

% cat <<"%EOF" > myfun.m # paste into shell if on UNIX
function myfun()
    import pkg.MyClass;
    o{1} = MyClass();

    x = cellfun(@(d) isa(d,'MyClass'),o);
    fprintf('cellfun: %d\n',x);

    b = isa(o{1},'MyClass');
    fprintf('direct: %d\n',b);

    fprintf('classes of o: \n');
    cellfun(@class, o,'UniformOutput',false)
end
%EOF 

+pkg/MyClass.m:

% mkdir "+pkg"; cat <<"%EOF" >"+pkg/MyClass.m" # paste to shell if on UNIX
classdef MyClass
end
%EOF

My Output is:

>> myfun
cellfun: 0
direct: 1
classes of o: 
ans = 
    'pkg.MyClass'

If I move MyClass.m to the same directory as myfun.m and remove the import ... line in myfun.m:

>> myfun
cellfun: 1
direct: 1
classes of o: 
ans = 
    'MyClass'
>> 
0

There are 0 best solutions below