Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation



06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

replaceserial(timerObject, replacement)
function timerObject = replaceserial(timerObject, replacement)
% REPLACESERIAL replaces anything in the timer properties that is a serial
% Note that since the timerObject is persistent, it is not strictly
% necessary to return the timer object at the end.

% checks the number of arguments
error(nargchk(1, 2, nargin))

% if there is only one argument, the replacement will be empty
if nargin == 1
    % set it
    replacement = [];

% check the timer object for validity
if any(~istimer(timerObject))
    % complain
    error('One or more timers is/are not valid.')

% checks the following parts
checkProperties = { 'StartFcn';...

% for each timer, we need to purge the serial objects from it
% (it'll break the save function otherwise) - this will not be
% effective if there is any weird nesting going on - this
% subfunction is not recursive.  have to use prod(size) for timers because
% they are stupid and numel doesn't work for them
for n = 1:prod(size(timerObject))
    % for each property
    for nn = 1:numel(checkProperties)
        % gets the property
        propertyToCheck = get(timerObject(n), checkProperties{nn});

        % if its a cell array, go through it
        if iscell(propertyToCheck)
            % finds out which bits are serial
            isSerialObject = cellfun('isclass', propertyToCheck, 'serial');

            % changes all of those to a dummy replacement if any applies
            if any(isSerialObject)
                % applies it
                propertyToCheck{isSerialObject} = replacement;

                % saves it back to the timer
                set(timerObject(n), checkProperties{nn}, propertyToCheck)

        elseif isstruct(propertyToCheck)
            % for each part (structfun does not work for
            % non-scalar structures)
            for nnn = 1:numel(propertyToCheck)
                % finds out which bits are serial
                isSerialObject = structfun(@(x) isa(x, 'serial'), propertyToCheck(nnn));

                % if there is anything...
                if any(isSerialObject)
                    % get the fieldnames
                    structFieldNames = fieldnames(propertyToCheck(nnn));

                    % for each fieldname
                    for nnnn = 1:numel(structFieldNames)
                        % if its a serial, change it
                        if isSerialObject(nnnn)
                            % change it
                            propertyToCheck(nnn).(structFieldNames{nnnn}) = replacement;

            % saves it back (even if nothing has changed -
            % can't easily check that in one line for
            % non-scalar structures)
            set(timerObject(n), checkProperties{nn}, propertyToCheck)

        elseif isa(propertyToCheck, 'serial')
            % simple replacement
            set(timerObject(n), checkProperties{nn}, replacement)

Contact us