GUI handle structure not updating

4 views (last 30 days)
Hadi Zyien
Hadi Zyien on 1 Mar 2019
Commented: Hadi Zyien on 1 Mar 2019
I have a simple GUI with a textbox where the user can enter a variable name, and then press the OK button or enter. The GUI then should check if the entered text is an acceptable variable name. My problem is that the pushbutton1_Callback function does not have the up-to-date information in handles.textbox1. Instead of the text that the user writes, I get an empty string and the warning is displayed. If I do pause(1) before the check, then the information is updated and all is fine (except for the unnessessary slow down). How can I get the update information?
% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(hObject, eventdata, handles)
key = get(gcf, 'CurrentKey');
if (strcmp(key, 'return'))
pushbutton1_Callback(hObject, eventdata, handles)
end
% --- Executes on key press with focus on textbox1 and none of its controls.
function textbox1_KeyPressFcn(hObject, eventdata, handles)
key = get(gcf, 'CurrentKey');
if (strcmp(key, 'return'))
pushbutton1_Callback(hObject, eventdata, handles)
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% Waiting allows handles to update, but it slows down the function!
% pause(1)
% Retrieving GUI data does not help!
handles = guidata(hObject);
% Check that name provided is valid
newName = get(handles.textbox1, 'String');
if isvarname(newName)
close(handles.figure1);
else
warning('Invalid variable name.')
end
Edit: If I delete textbox1_KeyPressFcn and figure1_KeyPressFcn and instead add figure1_WindowKeyPressFcn the issue still exists. The newName string is empty.
% --- Executes on key press with focus on figure1 or any of its controls.
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
key = get(gcf,'CurrentKey');
if (strcmp(key, 'return'))
pushbutton1_Callback(hObject, eventdata, handles)
end
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% Waiting allows handles to update, but it slows down the function!
% pause(1)
% Retrieving GUI data does not help!
handles = guidata(hObject);
% Check that name provided is valid
newName = get(handles.textbox1, 'String');
if isvarname(newName)
close(handles.figure1);
else
warning('Invalid variable name.')
end
  2 Comments
Hadi Zyien
Hadi Zyien on 1 Mar 2019
Edited: Hadi Zyien on 1 Mar 2019
Even if I delete the figure1_KeyPressFcn callback, and leave textbox1_KeyPressFcn for the textbox callback, the issue remains.
Deleting both those callbacks and using figure1_WindowKeyPressFcn also does not update the handles correctly.

Sign in to comment.

Accepted Answer

Dennis
Dennis on 1 Mar 2019
You can use drawnow to update your graphic elements:
function figure1_WindowKeyPressFcn(hObject, eventdata, handles)
key = get(gcf,'CurrentKey');
if (strcmp(key, 'return'))
drawnow
pushbutton1_Callback(hObject, eventdata, handles)
end

More Answers (0)

Categories

Find more on Programmatic Model Editing in Help Center and File Exchange

Products


Release

R2018a

Community Treasure Hunt

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

Start Hunting!