function strOut = cell2str(strIn, delim, quote)
%CELL2STR Convert a cell array of strings into a string.
%   OUT=CELL2STR(IN) converts a cell array of strings IN into a
%   space-delimited string OUT.
%   OUT=CELL2STR(IN, DELIM) converts a cell array of strings IN into a
%   string OUT delimited by DELIM.
%   OUT=CELL2STR(IN, DELIM, WRAPPER) same as above except each string is
%   wrapped in the specified WRAPPER character.

%   Copyright 2012-2017 The MathWorks, Inc.

strOut = '';

if isempty(strIn)
    return;
end

if ischar(strIn)
    strOut = strIn;
    return;
end

% assert(iscell(strIn), 'Input must be a cell string.');

num = numel(strIn);
if num==1
    strOut = strIn{1};
    return;
end

% Default delimiter: space
if nargin<2
    delim = ' ';
else
    nl = newline;
    canAddSpace = ~any(strcmp(delim, {'_',nl,''}));
    if canAddSpace
        delim = [delim ' '];
    end
end
% Default quote: none
if nargin<3
    quote = '';
end

assert(ischar(delim),    'Delimiter must be a string.');
assert(ischar(quote),    'Text wrapper must be a string.');

temp = char(strIn);
for i=1:num-1
    strOut = [strOut quote strtrim(temp(i,:)) quote delim]; %#ok<AGROW>
end
strOut = [strOut quote strtrim(temp(num,:)) quote ];

% LocalWords:  DELIM
