フィルターのクリア

How to use "contains" statement to detect a string without ambiguity?

7 ビュー (過去 30 日間)
Giuseppe
Giuseppe 2022 年 6 月 22 日
コメント済み: Steven Lord 2022 年 6 月 22 日
Hi to everyone,
I have a table with some data (attached file table.mat ). In the second column named " Sol_inters " I have three types of data (strings):
A
B
A & B
I want to filter the original table in order to create three filtered tables (tab_A, tab_B, tab_AB) in which their second column contains respectively "A", "B" and "A & B". So, for each table I want to mantain only rows that satisfy the condition on second column.
I 'm trying to use the "contains" statement but it fails with cases "Sol_filtered_A " and "Sol_filtered_B" becasue it maintans always also the case "A & B".
Here is my code:
% Import general solution
Sol_data = readtable('All_database_solution_test.csv','VariableNamingRule','preserve');
% Filter solutions: select if the solution occurs in point A, B or both
Sol_A = contains(Sol_data.Sol_inters,'A','IgnoreCase',false);
Sol_B = contains(Sol_data.Sol_inters,'A','IgnoreCase',false);
Sol_AB = contains(Sol_data.Sol_inters,'A & B','IgnoreCase',false);
Sol_filtered_A = Sol_data(Sol_A,:);
Sol_filtered_AB = Sol_data(Sol_AB,:);
Can you help me to solve this issue?

採用された回答

Stephen23
Stephen23 2022 年 6 月 22 日
編集済み: Stephen23 2022 年 6 月 22 日
"Can you help me to solve this issue?"
CONTAINS() check if the pattern occurs anywhere inside the string, but this could be a substring. So if you want match only the entire string then you need to use the correct tool: MATCHES() or STRCMPI()
  2 件のコメント
Giuseppe
Giuseppe 2022 年 6 月 22 日
編集済み: Giuseppe 2022 年 6 月 22 日
Hi @Stephen23, can you show me an example by using the code in my question?
Can you also tell me what is the difference bewteen two tools you mentioned?
Steven Lord
Steven Lord 2022 年 6 月 22 日
The documentation pages for the matches and strcmpi functions each contain multiple examples showing what each function does.
If you had to use contains (as a requirement of a homework assignment, for instance) remember that contains returns a logical value, one that you can use with the & (and), | (or), and ~ (not) operators.
true & false % false
ans = logical
0
true & ~false % true
ans = logical
1

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by