Code covered by the BSD License  

Highlights from
CO2gui - lab control and automation

CO2gui - lab control and automation

by

 

06 Jan 2010 (Updated )

Software used for controlling and data logging lab equipment.

findinstrument(mainGuiHandle, fieldName, match)
function index = findinstrument(mainGuiHandle, fieldName, match)
% FINDINSTRUMENT Finds an instrument number based on the chosen field.

% If the matching field is supplied, all of the unique current values are
% listed.

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

% checks each handle
if ~ishandle(mainGuiHandle)
    % errors
    error('mainGuiHandle must be a valid handle.')
    
elseif ~isstring(fieldName)
    % errors
    error('Field name must be a string.')
end

% match is not checked because it can be literally anything

% gets the object config and types
objectConfig = getappdata(mainGuiHandle, 'objectConfig');
objectTypes = getappdata(mainGuiHandle, 'objectTypes');

% if either of them are empty, error, because the handle musn't be valid
if isempty(objectConfig) || isempty(objectTypes) || ~isstruct(objectConfig) || ~isstruct(objectTypes)
    % errors
    error('Appdata is not in the handle object supplied.')
end

% trims off the pico log and the rheodyne
objectConfig = objectConfig(1:8);

% checks that that the field is in object config or in object types
if all(~strcmp(fieldName, fieldnames(objectConfig))) && all(~strcmp(fieldName, fieldnames(objectTypes)))
    % errors
    error('Invalid field name to search for.')
end

% concatenates the respective object types on to the object config - this
% means we can search for both types of fields

% for each item...
for m = 1:numel(objectConfig)
    % tack on the correct part of the structure (objectConfig takes
    % precedence)
    newObjectConfig(m) = catstructs(objectTypes(objectConfig(m).type), objectConfig(m));
end

% if the match was supplied, match the item
if nargin >= 3
    % tries to match the item
    index = find(cellfun((@(x) isequal(x, match)), {newObjectConfig.(fieldName)})');

else
    % list all of the unique values for that field
    index = unique({newObjectConfig.(fieldName)}');
end

Contact us