selectionChangedFcn repeatedly called after mouse click and key presses
8 ビュー (過去 30 日間)
古いコメントを表示
I am using MATLAB 2022b on Windows 10. With the following script and functions, if I left click on the rightmost radio button then press a key twice, the selectionChangedFcn is called endlessly: What did I do wrong?
fig = uifigure( ...
"WindowKeyPressFcn", @windowKeyPressFcn);
fig.UserData.buttonGroup = uibuttongroup(fig, ...
'SelectionChangedFcn', @selectionChangedFcn);
width = fig.UserData.buttonGroup.Position(3) / 2;
height = fig.UserData.buttonGroup.Position(4);
fig.UserData.control(1) = uicontrol(fig.UserData.buttonGroup, ...
'Style', 'radiobutton', ...
'Position', [1 1 width height]);
fig.UserData.control(2) = uicontrol(fig.UserData.buttonGroup, ...
'Style', 'radiobutton', ...
"Position", [(width+1) 1 width height]);
fig.UserData.controlIndex = 1;
function windowKeyPressFcn(source, event)
if source.UserData.controlIndex == 1
source.UserData.controlIndex = 2;
source.UserData.control(2).Value = 1;
else
source.UserData.controlIndex = 1;
source.UserData.control(1).Value = 1;
end
end % function windowKeyPressFcn
function selectionChangedFcn(source, event)
if event.NewValue.String == "1"
fprintf("1");
else
fprintf("2");
end
end % function selectionChangedFcn
0 件のコメント
回答 (1 件)
T.Nikhil kumar
2024 年 2 月 12 日
Hello Roberto,
According to the code, you have created a figure with two radio buttons inside a button group. When a user presses any key, the ‘windowKeyPressFcn’ is called, through which you want to toggle the selection between the two radio buttons. You are trying to achieve this by setting the ‘Value’ property of the radio buttons to 1 (which means selected) or 0 (which means not selected).
I would like to point out to you that programmatically changing the ‘Value’ property of a radio button triggers the callback corresponding to ‘SelectionChangedFcn’ property. If a key is pressed again while the ‘SelectionChangedFcn’ is executing, it can lead to a situation where it is going to be called endlessly.
For resolving this issue, I would recommend you to modify the ‘windowKeyPressFcn’ to check if the ‘Value’ of the radio button is already what it is being set to. If it is, the function should not do anything. This prevents the ‘SelectionChangedFcn’ from being called if the radio button is already in the desired state. Please look at the below code snippet for the new ‘windowKeyPressFcn’:
function windowKeyPressFcn(source, event)
if source.UserData.controlIndex == 1
if source.UserData.control(2).Value == 0 % Only change if not already selected
source.UserData.controlIndex = 2;
source.UserData.control(2).Value = 1;
end
else
if source.UserData.control(1).Value == 0 % Only change if not already selected
source.UserData.controlIndex = 1;
source.UserData.control(1).Value = 1;
end
end
end % function windowKeyPressFcn
Additionally, I assume that you intend for the ‘selectionChangedFcn’ to print "1" or "2" based on which radio button was selected. For this, I would suggest you modify ‘selectionChangedFcn’ to use ‘Tag’ property of the radio buttons to differentiate between them. Refer to the following code snippet for the modified ‘selectionChangedFcn’:
function selectionChangedFcn(source, event)
fprintf("%s", event.NewValue.Tag);
end % function selectionChangedFcn
Note that, the ‘Tag’ property is a user-defined identifier for UI components. This requires setting it’s value for each radio button as shown below, prior to usage:
fig.UserData.control(1).Tag = '1';
fig.UserData.control(2).Tag = '2';
Hope this helps you proceed further!
参考
カテゴリ
Help Center および File Exchange で Migrate GUIDE Apps についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!