table内の文字列の有無の存在を確認する方法

13 views (last 30 days)

Commented: Atsushi Ueno on 9 Aug 2022
テーブル内の任意の文字列の有無を確認する方法があれば教えて下さい。
0 CommentsShowHide -1 older comments

Sign in to comment.

Answers (1)

Hernia Baby on 9 Aug 2022

------------------------------
データを準備します
T = table(categorical({'M';'F';'M';'M'}),[45;32;34;54],...
{'NY';'CA';'MA';'CA'},logical([1;0;0;1]),...
'VariableNames',{'Gender','Age','State','Vote'})
T = 4×4 table
Gender Age State Vote ______ ___ ______ _____ M 45 {'NY'} true F 32 {'CA'} false M 34 {'MA'} false M 54 {'CA'} true
ここでCAという文字が何処かにないか探します
word = 'CA';
idx = table2array(varfun(@(x) strcmp(string(x),word),T))
idx = 4×4 logical array
0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0
find関数で何行目にあるのか探し、該当する部分を表示させます。
[x,~] = find(idx);
T(x,:)
ans = 2×4 table
Gender Age State Vote ______ ___ ______ _____ F 32 {'CA'} false M 54 {'CA'} true
1 CommentShowHide None
Atsushi Ueno on 9 Aug 2022

T = table(categorical({'M';'F';'M';'M'}),[45;32;34;54],...
{'NY';'CA';'MA';'CA'},logical([1;0;0;1]),...
'VariableNames',{'Gender','Age','State','Vote'})
T = 4×4 table
Gender Age State Vote ______ ___ ______ _____ M 45 {'NY'} true F 32 {'CA'} false M 34 {'MA'} false M 54 {'CA'} true
idx = find(strcmp(T.State,'CA')); % table の'State'列に'CA'がある行を検索
T(idx, :)
ans = 2×4 table
Gender Age State Vote ______ ___ ______ _____ F 32 {'CA'} false M 54 {'CA'} true

Sign in to comment.

Categories

Find more on Logical 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!