Search code examples
stringmatlabcell-array

Comparing subsequent string elements in cell array in MATLAB


I have a cell array Data<1048536x1> of strings in MATLAB. I would like to compare adjacent string elements in the cell array. Is there a function in MATLAB? I tried using strcmp but it only works when you have two strings not for adjacent strings in a cell array. Any help much appreciated.


Solution

  • To compare every string against the next string, use

    sameIdx = find(strcmp(Data(1:end-1),Data(2:end)));
    

    With this, the string at any position of sameIdx is the same as the string at position sameIdx+1. Note that this way, you don't need to check for the same previous string.