Felipe Gonzalez
Felipe Gonzalez

Reputation: 27

delete range of rows of a cell array under certain condition, MATLAB

I have a very large cell array containing a lot of measures. In general the measurements are in the range of 3 to 15 meters. My problem is that some of these measurements don't have this range, so it's invalid data, I want to remove these range of data from my cell array.

Here is what I have tried (in resume):

ind_cond = find(strcmp('Machine',A{:,1}));
A = table2cell(A);

for i = 1:(length(ind_cond)-1);
cond = ismember(A(ind_cond(i):ind_cond(i+1),11),'15');
if cond == 0
   A(ind_cond(i):ind_cond(i+1),11) = [];
    end
end

So first I search for the word 'Machine' because this is in all the headers so I can have the total number of measurements. Then I try to find the string '15' (I convert this later to num) on the range of the measurements, and if there is no '15' I want to delete that range of rows from the array. I get the following error:

"A null assignment can have only one non-colon index"

Many thanks

EDIT:

Here is a picture of how the data looks ( I don't know how to upload this, is a .csv file, sorry)

The 11 column is the important thing, here is the data that I'm interested. The problem is for example that some data sets (they are a lot, from 0.25 to 17 meters) are incomplete, because they don't have the value '15' so I want to delete the entire dataset in that case.

My first attemp was make something like this

for i = 1:(length(ind_cond)-1);
  if ind_cond(i+1,1)- ind_cond(i,1) < 30 ;
     A(ind_cond(i):ind_cond(i+1),:) = [];
     end
end

And it works well but this don't delete all the conflictive data, since I have one (1) very large data set that don't have '15', and the condition above can't eliminate it.

In the picture "What i want to delete" is an example of how are the conflictive data, and I want to delete all that data. Overview of data What i want to delete

Upvotes: 0

Views: 213

Answers (1)

CAta.RAy
CAta.RAy

Reputation: 514

If the intent is to remove the cells that don't have the string '15', you can do the following:

A = [{'TEST'} {'Machine'} ; ...
{'test1'} {'3'}; ...
{'test2'} {'7'}; ...
{'test3'} {'16'}; ...
{'test4'} {'15'} ; ...
{'test5'} {'1'}; ...
{'test6'} {'8'}];
machine_cell = A(:,2);
% keep only cells that where there in no '15'
new_A = A(contains(machine_cell,'15'),:);

The new cell array will be:

>> new_A =

  1×2 cell array

{'test4'}    {'15'}

The opposite, keep all cells that doesn't have '15' then just negate contains:

new_A = A(~contains(machine_cell,'15'),:);
>> new_A =

  6×2 cell array

    {'TEST' }    {'Machine'}
    {'test1'}    {'3'      }
    {'test2'}    {'7'      }
    {'test3'}    {'16'     }
    {'test5'}    {'1'      }
    {'test6'}    {'8'      }

Upvotes: 1

Related Questions