GUI handle structure not updating

3 Ansichten (letzte 30 Tage)
Hadi Zyien
Hadi Zyien am 1 Mär. 2019
Kommentiert: Hadi Zyien am 1 Mär. 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 Kommentare
Adam
Adam am 1 Mär. 2019
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 am 1 Mär. 2019
Bearbeitet: Hadi Zyien am 1 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Dennis
Dennis am 1 Mär. 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

Weitere Antworten (0)

Kategorien

Mehr zu Entering Commands finden Sie in Help Center und File Exchange

Produkte


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by