Search code examples
excelmatlabstrcmp

Extracting elements from matrix using strings subscript error


Hi I have a massive excel table and I want to extract values for specific compounds. Matrix repeats itself in 72 blocks, where vehicle-class is repeated with different road gradients and compounds and driving conditions.

    cb=[Vehicleclass_raw,Gradient_raw,Component_raw];
    size cb= 119952x3 cell
    Vehicleclass_airquis is string with 37 elements 

so I thought I sort it according to criteria gradient vehicle class and component and this is the code blow. it works for the first 30 elements and then it crashes and error message is Subscripted assignment dimension mismatch. outval_aq(i,:) = (rows). I cant figure out what the error is. Thanks for the help Matthias

    outval_aq = ones(37,72)*119953;
    for i=1:37
    rows = find(strcmp(cb(:,1),Vehclass_airquis(i)) & strcmp(cb(:,2),'0%') &     strcmp(cb(:,3),'NOx'));
    if ~isempty(rows)
    outval_aq(i,:) = (rows)
    end
    end

Solution

  • In a Matrix each row has the same number of elements. In your case you initialized outval_aq to be a 37x72 matrix but rows has only 60 elements. Solutions could look like:

    %pad rows with nans to make it the required size:
    rows(end+1:72)=nan
    outval_aq(i,:) = (rows)
    

    .

    %write only the first 60 elements to the matrix, leave the remaining untouched:
    outval_aq(i,1:numel(rows)) = (rows)
    

    Or you could change outval_aq to be a cell array which contains vectors.