Conditional case on strfind
3 views (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
dpb
on 9 Apr 2014
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)
Azzi Abdelmalek
on 9 Apr 2014
Edited: Azzi Abdelmalek
on 10 Apr 2014
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 Comments
Azzi Abdelmalek
on 10 Apr 2014
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)
on 10 Apr 2014
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)
0 Comments
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!