selectionChangedFcn repeatedly called after mouse click and key presses
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Risposte (1)
T.Nikhil kumar
il 12 Feb 2024
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!
Vedere anche
Categorie
Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!