Search code examples
stringmatlabindexingfindcell

Matlab find string in cell array, report rows


I'm looking for a range of rows containing a string 'chr1' in sorted cell array rawArrayData.textdata so that I can work with just the data in those rows (e.g. just coordinates on a given chromosome, chr1):

chromCols = find([rawArrayData.textdata{:,1}] == 'chr1'); Error using == Matrix dimensions must agree.

I presume the error is improper use of find. Is there a way to do this with cell arrays? Alternately, is there a way to convert instances of 'chrX' to X, convert that to a double and use find?

I used this answer as a starting point, if that helps.

I'm pretty new to this stuff - if there's any other info I can provide I will do so.

Thanks a lot.


Solution

  • If your rawArrayData.textdata is as below, you can do something like that:

    rawArrayData.textdata = {'chr4'; 'chr1'; 'chr2'; 'chr1' };
    
    chromCols = find(cellfun(@(s) strcmp(s, 'chr1') == 1, ...
                        rawArrayData.textdata));
    
    % chromCols = [2, 4]    
    
    % get only chromCols rows
    rawArrayData.textdata{chromCols, 1}