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.

mixedcell2mat(input, functionHandle)
function input = mixedcell2mat(input, functionHandle)
% MIXEDCELL2MAT converts a cell array of mixed data types into a matrix.
% Anything which is not a scalar number is converted to a NaN.  Function
% handle is an optional argument to further reduce the cell array down
% (typically @isscalar).

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

% only works on cell arrays
if ~iscell(input)
    % errors
    error('Not defined for anything other than cell arrays.')
    
elseif nargin >= 2 && ~isa(functionHandle, 'function_handle')
    % errors
    error('Function handle must be a function handle.')
end

% finds the things which are not numeric or logical or are empty to NaN
% (does not test for being scalar, cell2mat will weed those out if
% necessary)
mask = (~cellfun(@isnumeric, input) & ~cellfun(@islogical, input)) | cellfun('isempty', input);

% also applies the function handle if supplied
if nargin >= 2
    % applies it
    mask = mask | ~cellfun(functionHandle, input);
end

% replaces them
input(mask) = {NaN};

% then converts it into a matrix
try
    % tries
    input = cell2mat(input);
    
catch
    % if the function handle wasn't supplied or isn't 'isscalar', apply it
    % and try again
    if nargin == 1 || isequal(functionHandle, @isscalar)
        % tries again
        input(mask | ~cellfun(@isscalar, input)) = {NaN};
        
        % tries again
        input = cell2mat(input);
        
    else
        % error
        rethrow(error)
    end
end

Contact us