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.

num2cellstr(input, format)
function output = num2cellstr(input, format)
% NUM2CELLSTR works exactly like num2str, except works on arrays to produce
% cell arrays rather than character arrays

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

% checks the input
if ~isnumeric(input) && ~islogical(input) && ~iscell(input)
    % input must be a cell array
    error('Input to num2cellstr must be a matrix or a cell array.')

elseif nargin >= 2 && ~isstring(format) && (~isscalar(format) || ~isnumber(format))
    % format must be a string or a valid scalar number (precision)
    error('Second argument must be a string (format) or a scalar number (precision).')

% pre-allocates the answer
output = cell(size(input));

% for each element (could possibly do this better with an anonymous
% function, but then would lose the ability to nest things)

% if its a cell array...
if iscell(input)
    % for each element
    for m = 1:numel(input)
        % if the format was supplied...
        if nargin >= 2
            % applies it
            output{m} = num2str(input{m}, format);

            % don't
            output{m} = num2str(input{m});

elseif isnumeric(input) || islogical(input)
    % define a delimiter
    delimiter = '$';
    % if no format was supplied, define one
    if nargin < 2
        % define the default
        format = '%d';

    % sprintf the data with the delimiter
    output = sprintf([format, delimiter], input);

    % if nothing was returned an invalid format was specified
    if isempty(output)
        % errors
        error('Invalid format specified.');

    % Now farm it off to strread
    output = strread(output, '%s', 'delimiter', delimiter);

    % any problems?
    if numel(output) ~= numel(input) || (numel(output) == 1 && ~isempty(strmatch(format, output, 'exact')))
        % errors
        error('Invalild format string.');

    % reshape needed
    output = reshape(output, size(input));

Contact us