Extracting data cell that match specific strings in different columns

Hello!
I have a table, T, with the following variable names: Type, Ta, Sub, Day, Try, Data
I ask the person for these different inputs: inType, inTa, inSub, inDay, inTry.
% Example:
inType = 'aa';
inTa = '1';
inSub = '001';
inDay = 'ape';
I want to grab and save the cell Data, for the Table row that has the specific variable inputs in the Example (need to search for the matching strings).
More table, T, information:
Type: 90x2 % char
Ta: 90x1 % double
Sub: 90x1 % cell array of character vectors
Day: 90x1 % cell array of character vectors
Try: 90x1 % double
Data: 90x1 % cell
Can you please help?
Thank you in advance!

 採用された回答

Jon
Jon 2021 年 10 月 27 日
編集済み: Jon 2021 年 10 月 27 日
If I understand your problem correctly I think you could adapt the approach I show in the simple example below
% make example table
animal = {'cat' 'cat' 'dog' 'fish' 'cat' 'hamster'}'
size = {'large' 'small' 'large' 'small','large','large'}'
color = {'black','brown','white','red','striped','brown'}'
weight= [2.2,1.3,5,0.5, 1.75,0.2]'
T = table(animal,size,color,weight)
% find weight of large,striped, cat
query = table
query.animal = 'cat'
query.size = 'large'
query.color = 'striped'
% find the correct row in table
[found,idx] = ismember(query,T(:,1:3),'rows')% just use columns that need to be matched
% get the corresponding weight
w = T.weight(idx)

4 件のコメント

Jon
Jon 2021 年 10 月 28 日
The first argument returned by ismember is a logical, set to true if a match is found. If you think there is some chance that a match will not be found you should check this before trying to do further manipulations on idx. You could also check if idx == 0
Jon
Jon 2021 年 10 月 29 日
Hi, did this answer your question? Do you need any further ideas?
Nina Perf
Nina Perf 2021 年 11 月 12 日
Yes, Thank you so much!
Jon
Jon 2021 年 11 月 12 日
I'm glad that worked for you. Good luck with your project

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeText Data Preparation についてさらに検索

製品

リリース

R2021a

質問済み:

2021 年 10 月 27 日

コメント済み:

Jon
2021 年 11 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by