Getting numbers of entries in uitable

6 vues (au cours des 30 derniers jours)
Cristian Martin
Cristian Martin le 31 Mai 2022
Commenté : Voss le 31 Mai 2022
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?

Réponse acceptée

Voss
Voss le 31 Mai 2022
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 commentaires
Cristian Martin
Cristian Martin le 31 Mai 2022
Thank you for the answer!
Voss
Voss le 31 Mai 2022
You're welcome!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Produits


Version

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by