필터 지우기
필터 지우기

search for a word in a cell

조회 수: 3 (최근 30일)
Amr Hashem
Amr Hashem 2015년 7월 28일
댓글: Stephen23 2015년 7월 31일
I have a data which is 20*55 cell
and I want to search for a word in this cells
I search in column 51,52,53,54 & 55(which contain text)
so i have to repeat by search five times each in a column
I use this code:
[ndata text alldata] = xlsread('categ.xlsx','new');
j=1;
k=1;
D=alldata(:,51); % search in column 51
for H = 1:R % for loop to remove NaN
if isnan(D{H})
D{H} = '';
end
end
for E=1:R % from 1 : length of columns
B=~cellfun('isempty',regexp(D,'BATTERY')) ; % works well search for battery (uppercase) and returns 1 or 0
if (B(E) == 1) % if value of array greater than or equal 1
Defs(j)=E; % save its postion (E,1)
j=j+1;
else % save all arrays didn't have 'check' separately
not(k)=E;
k=k+1;
end
end
data=alldata(Defs,:); % show data contains 'battery'
I want to use or ( if the word is exist in 51 or 52 ....55) , to reduce time
HOW I CAN SEARCH IN THE FIVE COLUMNS AT ONCE?
  댓글 수: 4
Amr Hashem
Amr Hashem 2015년 7월 28일
several words
for example:
battery
power
ecg...
one per each search
Amr Hashem
Amr Hashem 2015년 7월 29일
I want to search in the 5 columns by a word (ex: battery...) , without needing to repeat search at each column separately.

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

채택된 답변

Stephen23
Stephen23 2015년 7월 29일
편집: Stephen23 2015년 7월 29일
It seems that you want to collect together the rows of the file data that contain a particular word (e.g. "Battery"). Here is how you can search particular columns (here I search columns [2,3,4]), how the NaN's can be ignored, and how the rows can be extracted for your output:
X = {'A','Battery','Cheese', NaN;...
'A','Bison', NaN, 'Battery';...
'A','Battery','Battery',NaN;...
'A','Bathtub','Chalk', 'Duck'};
%
Y = X(:,2:4); % columns [2,3,4]
idx = cellfun('isclass',Y,'char');
idx(idx) = ~cellfun('isempty',regexpi(Y(idx),'Battery'));
data = X(any(idx,2),:)
And the output shown in the command window excludes the rows that do not contain the word "battery" (i.e. the fourth row has been removed):
data =
'A' 'Battery' 'Cheese' [ NaN]
'A' 'Bison' [ NaN] 'Battery'
'A' 'Battery' 'Battery' [ NaN]
  댓글 수: 3
Amr Hashem
Amr Hashem 2015년 7월 30일
편집: Amr Hashem 2015년 7월 30일
What if I want to save the forth row (didn't contain "battery") in a separate place,for a further\another search?
I try:
Notdata = X(~idx,:);
but it didn't work...!!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Propulsion and Power Systems에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by