Getting numbers of entries in uitable

4 ビュー (過去 30 日間)
Cristian Martin
Cristian Martin 2022 年 5 月 31 日
コメント済み: Voss 2022 年 5 月 31 日
Hi guys,
I have a uitable for different entries datas, for each entry i want to count in three edit text boxes the numbers of rows wich contain different values
For each row where in the nineth column is find 'Corn'
For each row where in the nineth column is find 'Rice'
For each row where in the nineth column is find 'Flour'
I tried this but every time i change the entry with a row with different value it stops:
new_data is 1x33 3976 cell
the following is under a push button
column_name = new_data(:,9);
match_corn = strcmp(column_name,'Corn');
match_rice = strcmp(column_name,'Rice');
match_flour = strcmp(column_name,'Flour');
if match_corn == 1;
count_corn = length(match_corn);
set(handles.edit3,'String',count_corn);
end
if match_rice == 1;
count_rice = length(match_rice);
set(handles.edit4,'String',count_rice);
end
if match_flour == 1;
count_flour = length(match_flour);
set(handles.edit5,'String',count_flour);
end
could you tell me where is my mistake?

採用された回答

Voss
Voss 2022 年 5 月 31 日
column_name is a vector, so strcmp(column_name,'Corn') is a vector (of class logical), so when you do:
if match_corn == 1
that condition will be considered true only if all elements of match_corn are true, i.e., all elements of column_name are 'Corn'.
Instead, you want to count the number of non-zero elements of match_corn, which you can do with nnz:
column_name = new_data(:,9);
match_corn = strcmp(column_name,'Corn');
match_rice = strcmp(column_name,'Rice');
match_flour = strcmp(column_name,'Flour');
count_corn = nnz(match_corn);
set(handles.edit3,'String',count_corn);
count_rice = nnz(match_rice);
set(handles.edit4,'String',count_rice);
count_flour = nnz(match_flour);
set(handles.edit5,'String',count_flour);
  2 件のコメント
Cristian Martin
Cristian Martin 2022 年 5 月 31 日
Thank you for the answer!
Voss
Voss 2022 年 5 月 31 日
You're welcome!

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

その他の回答 (0 件)

製品


リリース

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by