tags:

views:

66

answers:

4

I have a java object (let's call it Foo) with a length() method.

In MATLAB, I want to write a function that accepts an array of these objects and works with it. My problem is that the usual method of writing a loop breaks down:

function doSomething(someArray)
    for i = 1:length(someArray)
        % do stuff with someArray(i)
    end

because in this case MATLAB decides "oh, that's a Java object; length(x) should be interpreted as x.length() since it has a length() method:

function printLength(someArray)
disp(length(someArray));

    ...

> foo = %%% get my handle to the Java Foo object %%%
> printLength([foo foo foo])
3
> printLength([foo foo])
2
> printLength([foo])
300000
% foo.length() gets called and returns 300000 or whatever

Is there a way to get around this?

A: 

hmm, this seems to work....

function printLength(someArray)
if (isscalar(someArray))
    L = 1;
else
    L = length(someArray);
end
disp(L);
Jason S
+1  A: 

Have you tried using the function NUMEL instead of the function LENGTH? That should give you the same results (i.e. the number of elements in the array), but avoid the problem with the overloaded length method.

gnovice
Or `size` function?
yuk
True on both. I wonder if there are any reserved methods, or whether MATLAB always dispatches to Java objects if there is a method of the name in question.
Jason S
+3  A: 

You can use a cell array instead of a regular array - this will work:

>> jPanel = javax.swing.JPanel;
>> length({jPanel,jPanel,jPanel})
ans = 
    3
>> length({jPanel,jPanel})
ans = 
    2
>> length({jPanel})
ans = 
    1
>> length({})
ans = 
    0

And in your loop, simply access someArray{i} instead of someArray(i)

Yair Altman
+3  A: 

You can use builtin() to force Matlab to use its own length(), numel(), or whatever functions, ignoring the Java class's method of the same name. Calling isscalar() or numel() will work most of the time, because Java classes tend not to define methods with those names. But if they do, you'll get the Java method and have the same problem as with length(). Using builtin() will be more general, working regardless of what methods the Java classes have.

>> foo = java.lang.String('foo');
>> builtin('length', [foo foo])
ans =
     2
>> builtin('length', [foo])
ans =
     1
>> length([foo])
ans =
     3
>> 

You could wrap it up like this.

function out = mlength(x)
%MLENGTH Array length, ignoring Java length() methods

% Test for isjava to avoid ignoring overriden length() methods in Matlab objects
if isjava(x)
   out = builtin('length', x);
else
   out = length(x);
end
Andrew Janke