Jason S
Jason S

Reputation: 189906

matlab: finding length of an array of java objects

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?

Upvotes: 3

Views: 347

Answers (4)

Andrew Janke
Andrew Janke

Reputation: 23908

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

Upvotes: 3

Yair Altman
Yair Altman

Reputation: 5722

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)

Upvotes: 3

gnovice
gnovice

Reputation: 125874

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.

Upvotes: 1

Jason S
Jason S

Reputation: 189906

hmm, this seems to work....

function printLength(someArray)
if (isscalar(someArray))
    L = 1;
else
    L = length(someArray);
end
disp(L);

Upvotes: 0

Related Questions