Question

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?

Was it helpful?

Solution 2

You probably defined your BytesAvailableFCN using function handle syntax without additional arguments, like this

s.BytesAvailableFCN = @myfun();

Instead, you need to define your callback using a cell array, as explained here in the documentation. For example,

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

handles must already be defined and in your workspace when that line is run.

OTHER TIPS

Serial port callbacks are different to GUIDE callbacks. In the case of a serial port callback your object handle is to the serial port object and the event is a serial event. There is no third argument, hence handles being undefined.

If you want to retrieve your GUI handles from within this function you'll need to do so explicitly, similar to the way you're retrieving the comport handle - incidentally getting comport this way is probably unnecessary since I'd imagine it's the same object the callback is already receiving as hObject.

Since in this case handles is GUIDE-specific data, the 'proper' way to retrieve it would be:

handles = guidata(gcf);

If your GUI has multiple figures, you may need to use findobj() instead of gcf() to get the right one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top