Match two rows value by a column value

조회 수: 2 (최근 30일)
Cristian Martin
Cristian Martin 2022년 6월 1일
댓글: Cristian Martin 2022년 6월 1일
Hi guys,
In a table I want to count all matching rows based of a value of a columns
If two rows have value '1' in column 34 and in column 9 have value 'Corn' set a count in an edit text
If two rows have value '2' in column 34 and in column 9 have value 'Corn' set another count in an edit text
and so on..
new_data=get(handles.uitable4,'Data');
index1= strcmp(new_data(:,34),'1', new_data(:,9),'Corn');
index2= strcmp(new_data(:,34),'2', new_data(:,9),'Corn');
count_total_corn = count(index1, index2);
set(handles.edit4,'string', count_total_corn)
index3= strcmp(new_data(:,34),'3', new_data(:,9),'Rice');
index4= strcmp(new_data(:,34),'4', new_data(:,9),'Rice');
count_total_rice = count(index3, index4);
set(handles.edit5,'string', count_total_rice)
Could you give me an ideea? The above doesn't work...thanks
  댓글 수: 1
Jan
Jan 2022년 6월 1일
"Doesn't work" is a lean description. Prefer to share the details of the problem, if you want others to solve it. Do you get an error message?

댓글을 달려면 로그인하십시오.

채택된 답변

Jan
Jan 2022년 6월 1일
편집: Jan 2022년 6월 1일
index1= strcmp(new_data(:,34),'1', new_data(:,9),'Corn');
This is gun-shot-programming. Please read the documentation of strcmp to find out, how this command is used. I guess you want:
index1= strcmp(new_data(:,34), '1') & strcpm(new_data(:,9), 'Corn');
But what type is new_data(:,34) ? Maybe you need {new_data(:,34)} ?
"count" sounds like it counts, but it does this for patterns in a string. Again: read count .
count_total_corn = sum(index1) + sum(index2);
% or
count_total_corn = nnz([index1, index2]);
  댓글 수: 4
Cristian Martin
Cristian Martin 2022년 6월 1일
편집: Cristian Martin 2022년 6월 1일
Sorry, I just answered to your question: But what type is new_data(:,34)?
Both 9 an 34 are cell
this are results for disp 9 and 34: 'Corn' and [1]
i'm wrong?
Cristian Martin
Cristian Martin 2022년 6월 1일
My output for column 34 is [1]. How can I transform this into '1' ?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2015a

Community Treasure Hunt

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

Start Hunting!

Translated by