フィルターのクリア

Guide handing objects to functions

3 ビュー (過去 30 日間)
Robert Worm
Robert Worm 2018 年 8 月 17 日
コメント済み: Robert Worm 2018 年 8 月 17 日
Using Guide I have two radiobuttons. When pushing one the other has to be unchecked. So far this is my solution:
function distButton_Callback(hObject, eventdata, handles)
checkThis = get(handles.distButton, 'Value');
checkThat = get(handles.fftButton, 'Value');
if checkThis == checkThat
set(handles.fftButton, 'Value', ~checkThis);
end
Since there are four button pairs a function would save a couple of lines.
Calling the function:
checkThis('distButton', 'fftButton', handles);
However combining handles. with a string does not seem to convert this to 'distButton'
> Reference to non-existent field 'this'.
function checkThis(this, that, handles)
checkThis = get(handles.this, 'Value');
checkThat = get(handles.that, 'Value');
if checkThis == checkThat
set(handles.that, 'Value', ~checkThis);
end
It's probably a simple matter of syntax..
  2 件のコメント
Adam
Adam 2018 年 8 月 17 日
Why are you not just placing the radio buttons inside a uibuttongroup which does the mutual exclusivity for you?
Robert Worm
Robert Worm 2018 年 8 月 17 日
Since this is my first time working with guide, I was simply not aware of that. Thanks for the advice.

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

採用された回答

ES
ES 2018 年 8 月 17 日
function checkThis(this, that, handles)
checkThis = get(eval(['handles.', this]), 'Value');
checkThat = get(eval(['handles.', that]), 'Value');
if checkThis == checkThat
set(eval(['handles.', that]), 'Value', ~checkThis);
end
  3 件のコメント
Stephen23
Stephen23 2018 年 8 月 17 日
編集済み: Stephen23 2018 年 8 月 17 日
Do NOT follow this answer! There is absolutely no point in using ugly eval like that, unless you want to force yourself into writing slow, complex, buggy, hard-to-debug code.
It is so easy to write simpler and much more efficient code:
function checkThis(this, that, handles)
checkThis = get(handles.(this),'Value');
checkThat = get(handles.(that),'Value');
if checkThis==checkThat
set(handles.(that),'Value',~checkThis);
end
And, as Adam already wrote, a uibuttongroup would be even simpler.
Robert Worm
Robert Worm 2018 年 8 月 17 日
Much appreciated. I will take this into account.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by