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.

catstructs2(varargin)
function output = catstructs2(varargin)
% Concatenate n 1x1 structures into a 1xn structure.  This creates empty
% fields for when the structures do not have identical fields.

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

% removes the empty elements
varargin = varargin(~cellfun('isempty', varargin));

% stores the number of arguments (MATLAB doesn't like you to change nargin)
numArgs = numel(varargin);

% are they all structures?
if ~all(cellfun('isclass', varargin, 'struct'))
    % drills down a level if it can
    if ~all(cellfun('isclass', varargin{1}, 'struct'))
        % errors
        error('All arguments must be structures.')
        
    else
        % redefine the varargin
        varargin = varargin{1};
        
        % and the number of arguments
        numArgs = numel(varargin);
    end
end

% starts
fields = cell(numArgs, 1);

% for each structure...
for n = 1:numArgs
    % get the fieldnames
    fields{n} = fieldnames(varargin{n});
end

% gets the unique fields (reshaping the cell array too)
fields = unique(cat(1, fields{:}));

% for each field...
for o = 1:numel(fields)
    % for each structure
    for oo = 1:numArgs
        % is that field in the structure?
        if isfield(varargin{oo}, fields{o})
            % saves it to the output
            output(oo).(fields{o}) = varargin{oo}.(fields{o});
            
        else
            % save a numerical blank
            output(oo).(fields{o}) = [];
        end
    end
end

Contact us