2

I'm trying to create a GUI using GUIDE, which reads a string through serial communication. After that it cuts out the needed numbers and puts it on the screen. I have created this function, which is executed every time when there is a line of data in the buffer of the COM port:

function out = intcon1(hObject, eventdata, handles)
global comPort;
a=fgetl(comPort);
disp(a);

a(a==' ') = '';
indexstart=strfind(a,'[');
indexend=strfind(a,']');
measureddata=a(indexstart(1):indexend(1));
commas=strfind(measureddata,',');

re1data=measureddata(2:(commas(1)-1));
re2data=measureddata((commas(1)+1):(commas(2)-1));
im1data=measureddata((commas(2)+1):(commas(3)-1));
im2data=measureddata((commas(3)+1):(commas(4)-1));
temp1data=measureddata((commas(4)+1):(commas(5)-1));
temp2data=measureddata((commas(5)+1):(commas(6)-1));

old_str=get(handle.re1, 'String');
new_str=strvcat(old_str, re1data);
set(handles.listbox8, 'String', re1data);

Now I'm trying to put the data into a listbox. This is just the first value. The problem is, that Matlab keeps saying, that the handles are not defined. But I cound already create a button which clears the listbox using the following code:

function clearlists_Callback(hObject, eventdata, handles)
% hObject    handle to clearlists (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
set(handles.listbox8, 'String', '');

Does anyone have any idea what the problem is and how to fix it?

4

2 回答 2

1

串行端口回调不同于GUIDE 回调。在串行端口回调的情况下,您的对象句柄是串行端口对象,并且事件是串行事件。没有第三个参数,因此handles是未定义的。

如果你想从这个函数中检索你的 GUI 句柄,你需要明确地这样做,类似于你检索comport句柄的方式 - 顺便说一下,comport这种方式可能是不必要的,因为我想它是同一个对象回调已经收到了hObject

由于在这种情况下handlesGUIDE-specific data,因此检索它的“正确”方法是:

handles = guidata(gcf);

如果您的 GUI 有多个图形,您可能需要使用findobj()而不是gcf()获得正确的图形。

于 2014-01-12T18:32:19.860 回答
0

您可能使用没有附加参数的函数句柄语法定义了 BytesAvailableFCN,如下所示

s.BytesAvailableFCN = @myfun();

相反,您需要使用元胞数组定义回调,如文档中所述。例如,

s.BytesAvailableFCN = {'myFun', handles};

handles运行该行时,必须已经定义并在您的工作区中。

于 2014-01-12T20:24:52.657 回答