Matlab GUI Error = "H must be the handle to a figure or figure descendent."

조회 수: 3 (최근 30일)
Burak Safa Berzener
Burak Safa Berzener 2019년 11월 30일
답변: Geoff Hayes 2020년 2월 11일
Hello, I get this error when I run my program. I'm thinking it's about external function which is serialEventHandler but I am not sure about that. Also when I delete guidata(hObject,handles) under the serialEventHandler, that code doesn't work well. Here is my code;
function start_communication_Callback(hObject, eventdata, handles)
if ~isempty(instrfind)
fclose(instrfind);
delete(instrfind);
end
handles.time_axes = zeros(1,100);
handles.receiving_data_yaw_axes = zeros(1,100);
handles.receiving_counter = 1;
handles.axis_counter = 0;
my_serial_port = serial(handles.selected_port,'BAUD', str2double(handles.selected_baudrate));
handles.my_serial_port = my_serial_port;
set(handles.my_serial_port, 'BytesAvailableFcnMode', 'byte');
set(handles.my_serial_port, 'BytesAvailableFcnCount', 6);
handles.my_serial_port.BytesAvailableFcn = {@serialEventHandler,handles};
fopen(my_serial_port);
set(findall(handles.communication_group, '-property', 'enable'),'Enable','off')
tic;
guidata(hObject, handles);
function serialEventHandler(hObject, eventdata, handles, bytesToRead)
receiving_data = fread(handles.my_serial_port,6);
handles.receiving_counter = handles.receiving_counter + 1;
disp(handles.receiving_counter);
set(handles.timer_example_edit,'string',num2str(toc));
handles.receiving_data_yaw_axes(1,handles.receiving_counter) = receiving_data(1);
handles.time_axes(1,handles.receiving_counter) = toc;
if handles.receiving_counter >= 100
handles.axis_counter = handles.receiving_counter - 100;
end
axes(handles.real_time_plot_axes);
handles.real_time_plot_axes.XLim = [handles.axis_counter handles.axis_counter+100];
handles.real_time_plot_axes.YLim = [0 100];
plot(handles.real_time_plot_axes,handles.time_axes,handles.receiving_data_yaw_axes);
drawnow;
set(handles.receive_yaw_edit,'string',num2str(receiving_data(1)));
set(handles.receive_pitch_edit,'string',num2str(receiving_data(2)));
set(handles.receive_roll_edit,'string',num2str(receiving_data(3)));
guidata(hObject, handles);

답변 (1개)

Geoff Hayes
Geoff Hayes 2020년 2월 11일
Burak - probably too late to help but in
function serialEventHandler(hObject, eventdata, handles, bytesToRead)
the hObject parameter is (probably) the handle to the serial object. That is why, when you call (from this function)
guidata(hObject, handles);
you get the "H must be the handle to a figure or figure descendent" error. When assigning the callback, you can pass the handle to the figure/GUI instead
handles.my_serial_port.BytesAvailableFcn = {@serialEventHandler,handles.figure1};
using the field from handles that corresponds to the GUI (I'm assuming it is figure1 in the above code). Your callback could then become
function serialEventHandler(hObject, eventdata, bytesToRead, hGui)
handles = guidata(hGui);
% etc.
You will want to confirm that the handle to the figure (hGui) is the last parameter passed into this callback. I'm assuming it is but that assumption could be wrong.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by