GUI handle structure not updating

7 ビュー (過去 30 日間)
Hadi Zyien
Hadi Zyien 2019 年 3 月 1 日
コメント済み: Hadi Zyien 2019 年 3 月 1 日
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
  1 件のコメント
Hadi Zyien
Hadi Zyien 2019 年 3 月 1 日
Thanks! This works.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpecifying Target for Graphics Output についてさらに検索

製品


リリース

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by