Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Losing information from guidata

Subject: Losing information from guidata

From: Laurens Witter

Date: 5 Sep, 2013 23:07:06

Message: 1 of 2

I'm trying to make a simple GUI to acquire continuously from a nat.instr. USB board. The GUI has a graph (DataWindow), a start and stop button (StartAcqButton & StopAcqButton) and a button with two textboxes to update the y-axis (UpdateYax, YMaxVal & YMinVal).

It initially runs smoothly, but has random errors when starting and stopping, or when updating the axis. This can happen the first time, or after several times starting and stopping, or axis updating.

Errormessages vary, but involve plot not being able to execute because x and y are different size, or involving invalid object handles.

Here's the (relevant) code:

function UpdateYax_Callback(hObject, eventdata, handles)
% hObject handle to UpdateYax (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mydata = guidata(SimpleAcq);
ymax = str2num(get(handles.YMaxVal,'string'));
ymin = str2num(get(handles.YMinVal,'string'));
mydata.lims = [ymin ymax];
guidata(SimpleAcq,mydata);
return

function StopAcqButton_Callback(hObject, eventdata, handles)
% hObject handle to StopAcqButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
mydata = guidata(SimpleAcq)
s = mydata.stream;
s.stop();
s.release();
mydata.stream = s;
guidata(SimpleAcq,mydata);
return

function StartAcqButton_Callback(hObject, eventdata, handles)
% hObject handle to StartAcqButton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%Start of data acquisition:
    mydata = guidata(SimpleAcq);
    s = daq.createSession('ni');
    devices = daq.getDevices;
    if isempty(devices)
        error('The Acquisition device seems to be unavailable');
    end
    if length(devices)>1
        error('Please only connect one device');
    end
    s.addAnalogInputChannel(devices.ID, 'ai1', 'Voltage');
    s.Rate = 10000; %10kHz sampling
    s.IsContinuous = true;
    lh = s.addlistener('DataAvailable',@(src,event)plotData(src,event,handles.DataWindow));
    s.startBackground();
    mydata.stream = s;
    guidata(SimpleAcq,mydata);
return

function plotData(src,event,target)
    mydata = guidata(SimpleAcq);
    if ~ishandle(target)
        target = gcf;
    end
    ishandle(target)
    size(event.TimeStamps)
    size(event.Data)

    plot(target,event.TimeStamps, event.Data);
    set(target,'Xlim',[min(event.TimeStamps) max(event.TimeStamps)]);
    set(target,'Tag','DataWindow');
    if ~isfield(mydata,'lims')
        lims = [-10 10];
    else
        lims = mydata.lims;
    end
    set(target,'Ylim',lims);
    mydata.lims = lims;
    guidata(SimpleAcq,mydata);
return




I'm at a complete loss here. The errors seem so random..... I tried figuring it out with diary and type since matlab really hangs with no means to regain control. My best guess is that there are two processes running at the same time and one overwrites guidata just when it is accessed by another part of the program. But, if that is the problem and then how to fix it......

Any help would be welcome!

Subject: Losing information from guidata

From: Laurens Witter

Date: 9 Sep, 2013 21:18:07

Message: 2 of 2

Okay, even though I didn't get any answers yet, I think I narrowed it down to the separate plotting function. It seemed the good idea since I could control the looks of the plot much more, but it is buggy it seems. I reverted to
lh = s.addlistener('DataAvailable',@(src,event)plot(handles.DataWindow,event.TimeStamps, event.Data));
and then using
s.NotifyWhenDataAvailableExceeds = 10000/(1000/range);
to control the x-axis. The y-axis I still can't controll, but I probably have to live with it for now. If anyone has a solution, I would still be very interested!

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us