views:

26

answers:

1

I want to ask how to check if a variable is table 1x8 or 8x1 of type logical? I know I can check the class of an array for logical like this:

strcmp(class(a),'logical')

I know I can get the size of table like this:

[h w] = size(a);
if(w==1 & h==8 | w==8 & h==1)

But what if table has more than 2 dimensions? How can I get number of dimensions?

+3  A: 

To get the number of dimensions, use ndims

numDimensions = ndims(a);

However, you can instead request size to return a single output, which is an array [sizeX,sizeY,sizeZ,...], and check its length. Even better, you can use isvector to test whether it's a 1-d array.

So you can write

if isvector(a) && length(a) == 8
disp('it''s a 1x8 or 8x1 array')
end

Finally, to test for logical, it's easier to write

islogical(a)
Jonas
Oh! Thanx! I was guessing dims(a) but there is no such function.
Miko Kronn
@Miko Kronn: Have a look at my edit for a possibly even better solution to your problem
Jonas
Miko Kronn
Jonas