Reputation: 165
This is a MATLAB GUI. And I have a while loop running. But while in the loop i need to use keyboard inputs which are a different callback. Is there a way or is it possible to execute that callback while it is in the loop?
Note: I am using GUIDE
Upvotes: 0
Views: 2105
Reputation: 7423
Yes, this is possible. You just need to get the character data from the keypress callback to the callback that is in a loop. One way of doing this is via the figure guidata.
For example, if your loop is running from a button callback and you want to see a keypress on the figure you could use the following:
Button Callback
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
fig = get(hObject,'Parent');
for i=1:1000
pause(0.01);
% Get the latest guidata
handles = guidata(fig);
if isfield(handles,'KeyData' ) && ~isempty(handles.KeyData)
fprintf(1,'Pressed : %s\n', handles.KeyData.Character);
% Clear the keydata we have now handled.
handles.KeyData = [];
guidata(fig,handles);
end
end
Figure keypress callback
% --- Executes on key press with focus on figure1 and none of its controls.
function figure1_KeyPressFcn(hObject, eventdata, handles)
% Store the keypress event data for use in the looping callback
handles.KeyData = eventdata;
guidata(hObject,handles);
Upvotes: 1