Code covered by the BSD License  

Highlights from
MATLAB para Programadores C/C++

image thumbnail

MATLAB para Programadores C/C++

by

 

Webinar's slides and demo files.

daqstopbutton_custom(fig,obj,varargin)
function varargout = daqstopbutton_custom(fig,obj,varargin)
%DAQSTOPBUTTON        Add a stop/start button to a data acquisition application
%
% DAQSTOPBUTTON(FIG,OBJ) adds a start/stop button to figure FIG.  This
% button can be used to start and stop data acquisition object OBJ.
% DAQSTOPBBUTTON will also delete OBJ when FIG is closed (i.e., it sets
% FIG's CloseRequestFcn to delete the object)
%
% DAQSTOPBUTTON(FIG,OBJ,'P1','V1','P2','V2', ...) specifies Property-Value
% pairs for configuring properties of the start/stop button.  Any valid
% property of a togglebutton can be specified.
%
% DAQSTOPBUTTON(HBUTTON,OBJ) turns existing uicontrol HBUTTON into a
% start/stop button.
%
% HBUTTON = DAQSTOPBUTTON(...) returns a handle to the start/stop button
%
% Note: This button does not "listen" to determine if your object changes
%   state.  For instance, if you have a finite number of samples
%   acquired after starting, the button will not reset automatically
%   when your object stops running.  Don't despair, since it is easy
%   enough to do on your own!
%
% Example:
%      fh = figure;                         % Create a figure
%      ai = analoginput('winsound');        % Create an input object
%      addchannel(ai,1);                    % Add a channel
%      set(ai,'TriggerRepeat',inf);         % Configure to run infinitely
%      set(ai,'TimerFcn','plot(peekdata(ai,500))'); % Each timer event will plot recent data
%      hButton = daqstopbutton(fh,ai);      % Add the stopbutton

%   Scott Hirsch 
%   shirsch@mathworks.com
%   Copyright 2003 The MathWorks, Inc. 

%Error checking
msg = nargchk(2,inf,nargin);
error(msg)

if ~all(isvalid(obj))
    error('Second input argument must be valid daq object')
end;

if ~ishandle(fig)
    error('First input argument must be handle to a figure or a uicontrol');
end;

% Check if user input handle to figure or handle to button
switch get(fig,'Type')
    case 'figure'

        % Create the button
        hButton = uicontrol(fig,'style','togglebutton',varargin{:});
    case 'uicontrol'
        hButton = fig;
        fig = get(hButton,'Parent');
        % In R14, it's possible that fig would return a handle to a panel, not a figure
        if ~strcmp(get(fig,'Type'),'figure')
            fig = get(fig,'Parent');
        end;
end;

%Check current state of the object
val = strcmp(obj.Running,'On');

if val==1       %Already running
    set(hButton,'String','Stop');
    set(hButton,'Value',1)
else
    set(hButton,'String','Start');
    set(hButton,'Value',0)
end;

set(hButton,'Callback',{@localStartStopObject,obj})

% Configure the CloseRequestFcn of the figure
setappdata(fig,'DaqStopButtonObject',obj)
cr = get(fig,'CloseRequestFcn');
cr = ['obj=getappdata(gcf,''DaqStopButtonObject'');stop(obj);delete(obj);' cr ';'];
set(fig,'CloseRequestFcn',cr);

% Return a handle to the button
if nargout
    varargout{1} = hButton;
end;


function localStartStopObject(hButton,action,obj)
% Callback for the start/stop button
val = get(hButton,'Value');
if val==1       %Pushed in
    set(hButton,'String','Stop');
    if all(isvalid(obj))
        start(obj)
        trigger(obj)
    end;
else
    set(hButton,'String','Start');
    if all(isvalid(obj))
        stop(obj)
    end;
    
    % Output current data
    handles = guidata(hButton);
    
    warning off MATLAB:warn_r14_stucture_assignment
    handles.output.time = get(handles.hLine1,'XData');
    warning on MATLAB:warn_r14_stucture_assignment % ToDo
    
    handles.output.voltage = get(handles.hLine1,'YData');
    handles.output.frequency = get(handles.hLine2,'XData');        % From magnitude
    handles.output.magnitude =get(handles.hLine2,'YData');
    
    guidata(hButton, handles);
    uiresume;    
    
end;

Contact us