matlabmatlab-table

Delete table row when number from one column is within a string in another column


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


Solution

  • 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).