Search code examples
matlabautoresizexlsread

dynamic column resizing in Matlab based on excel inputs


I want to read in some data from excel into Matlab say a 3x10 matrix. I need to distinguish whether the some cells are empty or blank in the read data. If empty, delete the row, if zero leave the matrix unchanged.

I am facing two problems with this: Matlab automatically truncates the leading empty cells in the matrix but leaves the empty cells within. Automatically resizing the matrix/array.


Solution

  • Imagine you have the following excel spreadsheet:

    enter image description here

    xlsread will read the sheet and fill empty cells with NaN:

    A = xlsread('data.xlsx')
    
    A =
    
         9     2     1
         3     9     8
         0   NaN     7
         3     4     0
    

    Finally you just need to filter out the rows containing NaNs:

    A = A(find(~any(isnan(A),2)),:)
    
    A =
    
         9     2     1
         3     9     8
         3     4     0