How to search for an exact string not just contains

238 ビュー (過去 30 日間)
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 9 月 12 日
コメント済み: Paul Safier 2021 年 12 月 21 日
Hiyah
I'm searching a cell array for an exact string, e.g the data I am searching contains both 'Las Vegas, NV' and 'North Las Vegas, NV' and I want specifically the coordinats of 'Las Vegas, NV'
the code I've used so far is:
Y = ~cellfun('isempty',strfind(cellstr(num),'Las Vegas, NV'));
(then [row col] = find(Y==1) )
how do I modify?
Thanks alot !

回答 (2 件)

the cyclist
the cyclist 2020 年 9 月 12 日
編集済み: the cyclist 2020 年 9 月 12 日
Use strcmp instead, which checks for identical matches.
loc = {'Las Vegas, NV','North Las Vegas, NV'};
match = strcmp(loc,'Las Vegas, NV')
  4 件のコメント
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 9 月 12 日
well i'm not sure how to implement it. my task is finding directions between arbitary cities, a and b.
i've done the stage of calulated the coordinates and then get these to navigate to the right place in the table of the data which gives the distance. my code has passsed the test for random cities who's string is such that one does not contain another. but i have came across this problem for the case of las vegas and las vegas north as in the original post. For e.g in arbritary language so far is:
function[distance] = get_distance(a,b)
[words,num,raw]=xlsread('Distances.xlsx');
X = ~cellfun('isempty',strfind(cellstr(num),a));
Y = ~cellfun('isempty',strfind(cellstr(num),b));
[row col] = find(X==1);
[r c]=find(Y==1);
a1=row(1);
a2=col(1);
a=[a1 a2]
b1=r(2)
b2=c(2)
b=[b1 b2]
c=[(a1+1),(b1+1)]
distance=raw{c(1),c(2)}
end
many thanks
Paul Safier
Paul Safier 2021 年 12 月 21 日
@the cyclist your answer was definitely what was needed. Thanks!

サインインしてコメントする。


Walter Roberson
Walter Roberson 2020 年 9 月 12 日
I'm searching a cell array for an exact string
Use ismember()
  7 件のコメント
Walter Roberson
Walter Roberson 2020 年 9 月 12 日
X = ismember((num),a));
1 1 0
2 -1
Count your open brackets.
Sara Ismail-Sutton
Sara Ismail-Sutton 2020 年 9 月 12 日
omg hahhaa, yeh got it from the other line anywauy, thanks alot

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCell Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by