How can I match a string in a cell array and then pull out data from the row with the match?

11 ビュー (過去 30 日間)
L
L 2018 年 11 月 8 日
編集済み: Nick 2018 年 11 月 9 日
Hello,
I have been struggling to solve this problem on my own after combing the internet for solutions. Here is my code below.
A = {'Red123' 'Bob' '08/11/2018' 'Blah'; 'Red456' 'Jim' '11/08/2018' 'Meh'; 'Blue123' 'Bean' '02/11/2018' 'MoreMeh';...
'Blue456' 'Dale' '05/05/2005' 'Nope'};
IDTag = 'Blue123';
check = strcmp(A,IDTag);
check returns a 4x4 logical array with a 1 where I would expect it. What I'd like to figure out how to do is to then take the data after the match in the row (in this case it would be A(3)). So I'd like to have my script find the match and then assign A(3,2) and A(3,3) to variables to be passed off elsewhere. How can I do this dynamically and autonomously so when I need to pass in variables to check this list, I can quickly match and pull out data?
To reiterate, I want to match on Blue123, and if it is a match, I want to assign 'Bean' and '02/11/2018' to two different variables for later use. If I later want to match a different IDTag, I want it to do the same thing.
Thank you!

採用された回答

L
L 2018 年 11 月 8 日
Go figure, a few minutes after posting the question I stumble across something elsewhere that helps me discover the answer I was looking for.
Solution to my question
[~ idx] = ismember(IDTag,A);
Name = B(idx,2);
Date = B(idx,3);
  1 件のコメント
madhan ravi
madhan ravi 2018 年 11 月 8 日
編集済み: madhan ravi 2018 年 11 月 8 日
A small correction I suspect it should be
Name = A(idx,2);
Date = A(idx,3);
from the above details you provided in the question because B is not defined in the above

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

その他の回答 (1 件)

Nick
Nick 2018 年 11 月 8 日
編集済み: Nick 2018 年 11 月 9 日
For now i am assuming the ID is always in the first column. You could have the logical matrix repeat itself and then use that as an index:
% your code
A = {'Red123' 'Bob' '08/11/2018' 'Blah'; 'Red456' 'Jim' '11/08/2018' 'Meh'; 'Blue123' 'Bean' '02/11/2018' 'MoreMeh';...
'Blue456' 'Dale' '05/05/2005' 'Nope'};
IDTag = 'Blue123';
check = strcmp(A,IDTag);
% change the check matrix to have the entire row the same as the first row
check = repmat(check(:,1), 1, size(check,2));
correspondingData = A(check);
name = correspondingData{2};
date = correspondingData{3};
alternatively you can also do:
index = find(check(:,1)==1);
correspondingData = A(index,:);
name = correspondingData{2};
date = correspondingData{3};
  2 件のコメント
L
L 2018 年 11 月 8 日
Both above didn't work for me, but it could have been a misunderstanding in the question as the results I got from your code gave me the IDs and not the information I was looking for after those IDs when they match.
I did figure it out though, below is what I was looking for.
Nick
Nick 2018 年 11 月 9 日
I just saw i made a mistake in the answer above (accidantly used A instead of correspondingData). Thats the reason it did not work, edited my answer to correct for this error. I'm glad you found a solution already.

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

カテゴリ

Help Center および File ExchangeLabels and Annotations についてさらに検索

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by