execute function if any evaluation is found to be false

조회 수: 1 (최근 30일)
avram alter
avram alter 2020년 11월 16일
댓글: avram alter 2020년 11월 16일
I have a bunch of variables, and my program checks to make sure they are equal:
if strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:))...
&& strcmp(TrueValMat{2,3}, BOX(3,:))...
&& strcmp(TrueValMat{2,4}, BOX(4,:))...
&& strcmp(TrueValMat{2,5}, BOX(5,:))...
&& strcmp(TrueValMat{2,6}, BOX(6,:))...
&& strcmp(TrueValMat{2,7}, BOX(7,:))...
&& strcmp(TrueValMat{2,8}, BOX(8,:))
set(handles.certifyButton, 'enable', 'on');
end
I know it is ugly, but it works. It indexes cells from an excel file (TrueValMat), and compares each cell to a value in an array (BOX). if they all match, a button is enabled in a GUI.
I want it to do the opposite. I want this to compare the cells from TrueValMat and BOX, and if any of them are found to be unequal, then the condition will be met, and the GUI button will be enabled. I don't care if only a single pair doesn't match, or all of them don't match. as soon as there is a logical 0 found, it trips the condition.
How would I do this?

답변 (2개)

Walter Roberson
Walter Roberson 2020년 11월 16일
if ~( strcmp(TrueValMat{2,1}, BOX(1,:))...
&& strcmp(TrueValMat{2,2}, BOX(2,:))...
&& strcmp(TrueValMat{2,3}, BOX(3,:))...
&& strcmp(TrueValMat{2,4}, BOX(4,:))...
&& strcmp(TrueValMat{2,5}, BOX(5,:))...
&& strcmp(TrueValMat{2,6}, BOX(6,:))...
&& strcmp(TrueValMat{2,7}, BOX(7,:))...
&& strcmp(TrueValMat{2,8}, BOX(8,:)) )
set(handles.certifyButton, 'enable', 'on');
end
  댓글 수: 3
Walter Roberson
Walter Roberson 2020년 11월 16일
If even one of the conditions is false then the && part would be false, and ~ of that would be true.
The only way for the chain to be false would be if all of the components of the && are true (that is, everything matches), and then not of that would be false.
De Morgan's Laws:
~(A|B) = (~A)&(~B)
~(A&B) = (~A)|(~B)
avram alter
avram alter 2020년 11월 16일
Ok, that brings up an issue I didn't think about. If all of them match, I want the button to be disabled. According to this fix, all of them matching would be enable it as well. Any way to avoid that?

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


Sulaymon Eshkabilov
Sulaymon Eshkabilov 2020년 11월 16일
if strcmp(TrueValMat{2,1}, BOX(1,:))==false...
&& strcmp(TrueValMat{2,2}, BOX(2,:))==false...
&& strcmp(TrueValMat{2,3}, BOX(3,:))==false...
&& strcmp(TrueValMat{2,4}, BOX(4,:))==false...
&& strcmp(TrueValMat{2,5}, BOX(5,:))==false...
&& strcmp(TrueValMat{2,6}, BOX(6,:))==false...
&& strcmp(TrueValMat{2,7}, BOX(7,:))==false...
&& strcmp(TrueValMat{2,8}, BOX(8,:))==false
set(handles.certifyButton, 'enable', 'on');
end
  댓글 수: 1
avram alter
avram alter 2020년 11월 16일
Won't this onto trigger if all are found to be false? I want the condition to be triggered even if only a single condition is found false

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

카테고리

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

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by