フィルターのクリア

HELP: Find All Rows of a table which meet a specific condition

641 ビュー (過去 30 日間)
Nikos Anninos
Nikos Anninos 2018 年 5 月 4 日
回答済み: Alon Zaharony 2022 年 7 月 18 日
Hello everyone and thanks a lot in advance for your assistance.
Please allow me to introduce you to the problem:
I have an excel file where in the first column there are different students' names. Also, in the second and third column, there are the names of their hometowns and their class level, respectively.
For example:
George London Class B1
George Liverpoool Class B2
Andrew London Class C1
... ... ...
Mairy London Class C2
All these data were imported into Matlab via readable command (because with the xlsread I was getting NAN values).
Now what I need to do is a formula which will find all those students that live in 1 specific town. Let that town be London.
Next, from the previously created table, I need to find those students which belong to Class B1, B2, C1 and C2 respectively. Let B2 to be selected class for this example:
My code is as below:
Alldata = readtable('alldata.xlsx'); Hometown = alldata(:, 14); ==> The hometowns are in column 14.
Class = alldata(:, 13); ==> The classes are in column 13.
a = 'London';
b = 'Class B2';
Result1 = find(alldata.HOMETOWNS == 'a');
Relust 2 = find(alldata.CLASSES == 'b');
FinalTable = table(Relult1, Relust2);
I though that this should worked fine but I get the error: Undefined operator '==' for input arguments of type 'table'.
I also changed parenthesis to braces, the table(s) to cell arrays but nothing worked for me. Unfortunately, I have not too much experience with Matlab sofware, and hence, you are kindly requested to help me with this one cause I really don't have a clue about what is wrong.
I look forward to your reply.
Thanks a lot in advance guys,
Nick A.
  1 件のコメント
Emil Stobbe
Emil Stobbe 2020 年 4 月 3 日
Hi,
this was already really useful thanks! However I want to do the same thing but my condition is not a string but a subject number like 101. I cant get it to work. I figured that strcmp is not the right function but I could not find any other alternative.
Thanks in advance,
Best Wishes

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

採用された回答

Guillaume
Guillaume 2018 年 5 月 8 日
find is overused by newcomers to matlab. find is very rarely needed and it is certainly not needed in your case. That's unrelated to your problem though, it makes your code less efficient.
Your problem is that you cannot use == to compare char arrays. The proper way to compare char arrays is to use strcmp
FinalTable = alldata(strcmp(alldata.HOMETOWNS, 'London') & strcmp(alldata.CLASSES', 'Class B2'), :)
Note that if your intent is to split your table into several tables according to the class and hometown, then a) you shouldn't, and b) there are lot more efficient ways to do it than just copy/pasting the above and changing the condition each time.
  3 件のコメント
Emil Stobbe
Emil Stobbe 2020 年 4 月 3 日
Hi,
this was already really useful thanks! However I want to do the same thing but my condition is not a string but a subject number like 101. I cant get it to work. I figured that strcmp is not the right function but I could not find any other alternative.
Thanks in advance,
Best Wishes
Guillaume
Guillaume 2020 年 4 月 3 日
If you're trying to match numerical, logical or categorical values, then simply use the standard == comparison
FinalTable = yourtable(yourtable.SubjectNumber == 101, :)

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

その他の回答 (3 件)

Peter Perkins
Peter Perkins 2018 年 5 月 14 日
Convert thos variables to categorical, something like
alldata.HOMETOWNS = categorical(alldata.HOMETOWNS);
Then
i = (alldata.HOMETOWNS == 'a') && (alldata.CLASSES == 'b');
alldata(i,:)

ASJ
ASJ 2018 年 5 月 8 日
Hi, try something like this:
Result01=alldata(alldata(:,14) =='London',:)
Result02=alldata(alldata(:,13) =='Class B2',:)
  2 件のコメント
Guillaume
Guillaume 2018 年 5 月 8 日
You cannot use == to compare char arrays.
LU Chongkai
LU Chongkai 2020 年 3 月 31 日
changing the inner parentheses to brace make it works

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


Alon Zaharony
Alon Zaharony 2022 年 7 月 18 日
Hi,
See the following correspondance. It works for me:

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by