Conditional case on strfind

조회 수: 14 (최근 30일)
Jacky
Jacky 2014년 4월 9일
편집: Azzi Abdelmalek 2014년 4월 10일
Hi all,
I have a problem on with the strfind in matlab:
x =
{
'my height is 160'
'my height is 163'
'my height is 167'
'my height is 180');
feature_1 = find(~cellfun('isempty',strfind(x,'my height is 163')))
the output will be:
feature_1 =
2
Now my problem is how do i find for "my height is > 163" ?
Thanks.

답변 (3개)

dpb
dpb 2014년 4월 9일
Not easily by character matching/searching. You'd need to convert the numeric value strings in the and then do the search on that. SOTOO (warning, air code, untested)...
find(cellfun(@(x) str2double(x(end-3:end)),x)>163)
  댓글 수: 1
Jacky
Jacky 2014년 4월 10일
Thank you so much :), it works! This is checking for the last 3 numbers which are greater than 163 right? How if the x is like this:
x = {'my height is 160'
'my height is 163'
'my height is 167'
'my height = "180"'};
then the last cell cannot be matched because of the quotation marks before and after the number? Any way to solve this?
Thank you.

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


Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 9일
편집: Azzi Abdelmalek 2014년 4월 10일
x ={ 'my height is 160'
'my height is 163'
'my height is 167'
'my height is 180'};
num=regexp(x,'[0-9]+(\.)?[0-9]?','match')
out=x(str2double([num{:}])>163)
  댓글 수: 2
Jacky
Jacky 2014년 4월 10일
편집: Jacky 2014년 4월 10일
Thanks so much for your reply. I'm also using regular expression to solve this. But, if the x is like this:
x = {'my height is 1'
'my height is 13'
'my height is 5'
'my height = "2"'};
A = find(~cellfun('isempty',regexp(x, 'height = "[0-5]"', 'match')));
B = find(~cellfun('isempty',regexp(x, 'height is [0-5]', 'match')));
The output is:
>> B
B =
1
2
3
>> A
A =
4
The output for B is incorrect, because 'my height is 13' is included, but this is not suppose to included as 13 is not within the range of [0-5]. What is the mistake i made in the regular expression there?
Thank you.
Azzi Abdelmalek
Azzi Abdelmalek 2014년 4월 10일
This is not my answer. don't change [0-9],
num=regexp(x,'[0-9]+(\.)?[0-9]?','match')
a=str2double([num{:}])
The result is
1 13 5 2
Now we find the range 0-5
out=x(a>0 & a<=5 )

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


Jos (10584)
Jos (10584) 2014년 4월 10일
Approach this in two steps:
Step 1. Convert the strings to a numerical values. This step depends on how the strings are made up. If there is always one and only one value in each string, you can do something like this:
s = regexp(x,'\d+','match')
num = str2double([s{:}])
Step 2. Use logical indexing to get the indices into x
tf = num > 2 & num <= 5 % whatever
indices = find(tf)
result = x(tf)

카테고리

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