Return the number of rows of an array of cell data

조회 수: 7 (최근 30일)
charles atlas
charles atlas 2011년 12월 8일
I have an array of cell data “a” in Matlab. I want to look at the array (which contains numeric and text data) and say in Matlab speak: Find what row of “a” says: ‘Temp Height (density)’.
The Code will return b=1326; I was thinking something like:
[row,col] = find(a, {Temp Height (density)});
This will then give me the value row=1326 which is what I need. I tried to use the find function but it doesnt seem to work becasue it tells me: Undefined function or method 'find' for input arguments of type 'cell'.
Help on this is much appreciated.

채택된 답변

Sven
Sven 2011년 12월 8일
If your cell contains strings (and not numeric data), just use strcmp:
[row,col] = find(strcmp('Temp Height (density)', a));
If some elements of your cell are numeric, the above will fail ( UPDATE: Actually, the above works fine even for non-char elements... I'll leave the rest of my answer for reference). Another method uses the cell function below:
[row,col] = find(cellfun(@(c)ischar(c)&&strcmp('Temp Height (density)',c),a))
or do it in two steps:
strInds = find(cellfun(@ischar, a));
matchMask = strcmp('Temp Height (density)'), a(strInds));
[row,col] = ind2sub(size(a), strInds(matchMask));
  댓글 수: 1
charles atlas
charles atlas 2011년 12월 8일
That strcmp function worked perfectly, Thanks.
I can't believe there was such a simple solution to this problem.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Paulo Silva
Paulo Silva 2011년 12월 8일

Walter Roberson
Walter Roberson 2011년 12월 8일
Is the text to appear anywhere in the row of the cell array (i.e., text might be intermixed without any order), or is it to appear in a very specific column (e.g., column 3) of the cell array, or is it to appear in any of several known columns?
Is the text a substring of the text that is there? A leading substring? A trailing substring? Always the complete string?
If it is a very specific column, say 3, and forms the whole of the column, then
[tf, rowidx] = ismember(Temp Height (density), a(:,3));

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by