Search code examples
matlabmatlab-guidehandles

Can't access handles from a function


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?


Solution

  • 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.