1

Below presented a table where the third column is a cell array of doubles and the last column is a cell array of chars. I would like to delete row number 248 based on the condition that 'Subject moved a bit at the beginning of 2nd minute' contains the digit 2, which equals to the value of the third column of this row. I have implemented it by the following:

commentMinNum = regexp(cellfun(@string, T2.comments(:)),'\d','Match');
commentMinNumInd = find(~cellfun(@isempty, commentMinNum));
extractMinNum = cell2mat(cellfun(@double, commentMinNum, 'UniformOutput', false));
deleteCond = T2.minNum(commentMinNumInd) == extractMinNum;
T2(commentMinNumInd(deleteCond), :) = [];

This implementation seems complex and wordy for such a simple task. I would like to know if there is a more straightforward approach. Do I miss anything or Matlab wants me to suffer? :)

enter image description here

Wolfie
  • 27,562
  • 7
  • 28
  • 55
Gideon Kogan
  • 662
  • 4
  • 18

1 Answers1

2

Given table t:

name = {'TLVPivotal07';'TLVPivotal07';'TLVPivotal07'};
regularityStatus = [1;1;0];
minNum = [1;2;3];
comments = {'Subject moved a bit at the beginning of the 2nd minute';
'Subject moved a bit at the beginning of the 2nd minute';
'Subject moved a bit at the beginning of the 2nd minute'};

t = table(name,regularityStatus,minNum,comments);

A possible solution which uses regex to check whether the value in the second column is in the comments string:

indx = (1:numel(t.comments)).';
tInd = ~cellfun(@isempty,arrayfun(@(x,y) regexp(t.comments(y), [num2str(x) '[a-z]{2} minute'],'match','once'),t.minNum,indx));
t(tInd,:) = []

will eliminate the desired row(s).

Paolo
  • 21,270
  • 6
  • 38
  • 69
  • t(arrayfun(@(x,y) contains(x, y) ,t.comments, string(t.minNum)), :) = [] will not work in case of other numbers in my string? – Gideon Kogan Nov 06 '19 at 12:48
  • 2
    Well, `contains` will check whether the character (the number) does exist anywhere in the string. My solution will only look for the number followed by `nd minute`, `rd minute`, `th minute`. – Paolo Nov 06 '19 at 12:52