Search code examples
matlabcell-array

How to determine if all cell array elements are nonzero or true? Or: What is the cell array equivalent for "all"?


I'm looking for a fast way to evaluate whether any element of a cell array contains the logical value false. For matrix arrays there is the convenient function all which is not working for cells. I have a hard time to implement the same function for cell arrays without using loops, this solution would work, but shouldn't be an option. Any hints?

My cell array looks like that:

values = {  vector of values  ;  value  ; ...  ;  false  ;  ... ; value }

so basicallly I write the results of the process in a cell array, if there is an error in the process it returns a logic false. In the following I need to implement an error handling in case there was any error writing false

I'd really appreciate something simple like:

if ~all(values), do something to handle error; return; end

but for cell arrays...

Edit: if a cell element is a double with the value 0 it is a valid case and it shouldn't return false .


Solution

  • Check if it is logical and then check if it is false

    cellfun(@(x) islogical(x) && ~x, values)
    

    This is true where you have a false so you will need

        if any(cellfun(@(x) islogical(x) && ~x, values))
             %do something to handle error; 
             return; 
        end