Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Why error using cell2mat ?

Asked by saharsahar on 19 Jul 2013

Hi all, I have a 309 * 5956 cell array of same type and same length. however most of the elements are empty.

However using cell2array to convert this matrix to numerical it gives me error as:

cell2mat(all_i_dis)
Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 79)
        m = cat(1,m{:});

any idea is highly appreciated.

0 Comments

saharsahar

Products

No products are associated with this question.

1 Answer

Answer by Azzi Abdelmalek on 19 Jul 2013
Accepted answer
A={ 1 , 2 ; [], 4};
cell2mat(A);

The result

Error using cat
Dimensions of matrices being concatenated are not consistent.
Error in cell2mat (line 87)
      m = cat(2,m{:});

To use cell2mat, all cells must be the same size and same class

3 Comments

saharsahar on 19 Jul 2013

Thanks Azzi, so if I am right I have to convert all of the [] to 0 maybe? Is there anyway to do that?

Thanks

saharsahar on 19 Jul 2013

I know the way through a loop like

for n=1:length(mycellarray)
    if isempty(mycellarray{n})
       mycellarray{n}=0;
    end
end

but is there any other efficient way instead of loop?

Azzi Abdelmalek on 19 Jul 2013
A(cellfun('isempty',A))={0}

%or

A(cellfun('isempty',A))={nan}
Azzi Abdelmalek

Contact us