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 Center および File ExchangeCode Execution についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by