Disable Edit Text with Checkbox

Hello Everybody, I need your Help. I build a GUI and I want to activate or deactivate a Edit Text Box with the Help of a Checkbox. The following is my Code for this Problem:
WN = get(handles.White_Noise,'Enable');
if WN == 'on'
set(handles.WN_per,'enable','on');
set_param('Physikalisches_Modell/Noise_Enable','Value',num2str(1));
else WN == 'off'
set(handles.WN_per,'enable','off');
set_param('Physikalisches_Modell/Noise_Enable','Value',num2str(0));
WN_per = 0;
end
White_Noise is the Checkbox, WN_per is the Edit Text. The Part, to activate the Edit Text Box works perfectly fine, but i cannot deactivate it with pressing it again.
There was another Topic for this Problem, where the User hat a similar Code and it worked, when he Startet the GUI with the Command Window. Unfortunatly, this is not the Solution.
Can anyone help me?
Tanks, Jan

回答 (1 件)

Adam Danz
Adam Danz 2018 年 7 月 26 日
編集済み: Adam Danz 2018 年 7 月 26 日

0 投票

For both conditionals, instead of
if WN == 'on'
use
if strcmp(WN, 'on')
See comments below. You want to condition on the 'Value' property, not the 'Enable'.

4 件のコメント

Jan Lützenkirchen
Jan Lützenkirchen 2018 年 7 月 26 日
Thanks for your Answer, but that didn't work out.
I tried to see what exactly happens, so i let it me show the Value of the handles 'Enable'.
It doesn't matter, if i check or uncheck the Box, it always says 'on'. But I don't know, how to fix this Problem .
Adam Danz
Adam Danz 2018 年 7 月 26 日
編集済み: Adam Danz 2018 年 7 月 26 日
Ok, even though that didn't fix your problem, strcmp() is the way to compare strings; not with '=='.
I think I see what the problem is. If 'White_Noise' is the checkbox, you're conditioning on whether the checkbox is enabled or not rather than value of the check box. You want the 'Value' property, not the 'Enable'.
Change the appropriate lines to this
WN = get(handles.White_Noise,'Value');
if WN
...
else
...
end
Jan Lützenkirchen
Jan Lützenkirchen 2018 年 7 月 27 日
Thank you very much! Now that I know the Answer, its pretty obvious, that I mistake the handles I used.
I want to add, that the 'Value' gives a number, 0 or 1. So in the if-condition, i set
if WN == 1
....
else if WN == 0
....
end
Adam Danz
Adam Danz 2018 年 7 月 28 日
編集済み: Adam Danz 2018 年 7 月 28 日
The value property is a logical. So all you need is
if WN
if ~WN

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

製品

リリース

R2018a

質問済み:

2018 年 7 月 26 日

編集済み:

2018 年 7 月 28 日

Community Treasure Hunt

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

Start Hunting!

Translated by