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

3 次查看(过去 30 天)
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