GUI handle structure not updating

3 次查看(过去 30 天)
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 个评论
Adam
Adam 2019-3-1
I suspect your use of a KeyPressFcn is getting tangled up with the user typing in the edit box, though I am not sure.
You don't need to do this to just get behaviour on pressing 'Enter', just create a callback for your textbox1 instead, although this will also trigger when the control loses focus rather than only when you press Enter of click Ok.
You may also be able to use WindowsKeyPressFcn instead, but I don't remember exact details of exactly what causes problems where with key press functions and edit boxes.
Hadi Zyien
Hadi Zyien 2019-3-1
编辑:Hadi Zyien 2019-3-1
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.

请先登录,再进行评论。

采纳的回答

Dennis
Dennis 2019-3-1
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by