execute function if any evaluation is found to be false

1 Ansicht (letzte 30 Tage)
avram alter
avram alter am 16 Nov. 2020
Kommentiert: avram alter am 16 Nov. 2020
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?

Antworten (2)

Walter Roberson
Walter Roberson am 16 Nov. 2020
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 Kommentare
Walter Roberson
Walter Roberson am 16 Nov. 2020
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 am 16 Nov. 2020
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?

Melden Sie sich an, um zu kommentieren.


Sulaymon Eshkabilov
Sulaymon Eshkabilov am 16 Nov. 2020
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 Kommentar
avram alter
avram alter am 16 Nov. 2020
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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Code Execution finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by