Finding Indices of Cells Containing Certain Text in Mixed Arrays (Both Numbers and Strings)

11 次查看(过去 30 天)
I sometimes have to deal with excel files that contain columns of the following format:
i.e. a combination of numbers-only and number+text cells. What I need to do is to find cells that contain only the number '22' and have the array numbers in a new array. In this case the new array should be: [1 7 13 14 15]. Then I would like to create another array that contains the row numbers of the cells that contain the exact string '22 EZ'. In the case of this example that new array should be: [2 3 4 8]. How can I do this? So far I have tried to read the imput file using : [NUMx,STRx,RAWx]=xlsread('Inputfile.xlsx') But I am not sure which class I need to use in order to perform the search properly.
Saeid

采纳的回答

Stephen23
Stephen23 2016-12-17
编辑:Stephen23 2016-12-17
Untested, but something like this should work:
[NUMx,STRx,RAWx]=xlsread('Inputfile.xlsx')
find(cellfun(@(x)isnumeric(x)&&x==22,RAWx))
find(cellfun(@(x)ischar(x)&&strcmp(x,'22 EZ'),RAWx))
for example:
>> X = {22;'22 EZ';'22 EZ';'22 EZ';25;'TOLOUL';22;'22 EZ';25;'N 10';22;25;22;22;22;'N415'};
>> find(cellfun(@(x)isnumeric(x)&&x==22,X))
ans =
1
7
11
13
14
15
>> find(cellfun(@(x)ischar(x)&&strcmp(x,'22 EZ'),X))
ans =
2
3
4
8
  3 个评论
Saeid
Saeid 2016-12-18
编辑:Saeid 2016-12-18
Hi Stephen,
I can already use the method you mentioned, but sometimes the phrase '22 EZ' is just part of a larger phrase. The program in this form seems to only recognize the exact phrase '22 EZ' and if it is longer in the original excel cell (e.g. 'LAB 22 EZ 11_14') it will not find it. Is there a solution for that too?
Saeid

请先登录,再进行评论。

更多回答(0 个)

类别

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