View License

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video

Highlights from
STRJOIN

Join the 15-year community celebration.

Play games and win prizes!

» Learn more

5.0
5.0 | 5 ratings Rate this file 25 Downloads (last 30 days) File Size: 2.61 KB File ID: #31862 Version: 1.1

STRJOIN

by

 

18 Jun 2011 (Updated )

Concatenate an array into a single string.

| Watch this File

File Information
Description

Syntax
    S = strjoin(C)
    S = strjoin(C, separator)
 
Description
    S = strjoin(C) takes an array C and returns a string S which
    concatenates array elements with comma. C can be a cell array
    of strings, a character array, or a numeric array. If C is a
    matrix, it is first flattened to get an array and concateneted.
    
    S = strjoin(C, separator) also specifies separator to be used
    for string concatenation. The default separator is comma.
 
Example
    >> str = strjoin({'this','is','a','cell','array'})
    str =
    this,is,a,cell,array
    >> str = strjoin(char({'this','is','a','char','array'}))
    str =
    this,is,a,char,array
    >> str = strjoin([1,2,2],'_')
    str =
    1_2_2
    >> str = strjoin({1,2,2,'string'},'\t')
    str =
    1 2 2 string

MATLAB release MATLAB 7.12 (R2011a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (6)
12 May 2016 Mazen Saied

Thanks Kota,
I did a small modification so that the separator can be a cell of strings.

str = strjoin({{'a' 1} {'b' 2}}, {'\n' ': '})
str =
a: 1
b: 2

Hope it can help.

function output = strjoin(input, separator)
%STRJOIN Concatenate an array into a single string.
%
% S = strjoin(C)
% S = strjoin(C, separator)
%
% Description
%
% S = strjoin(C) takes an array C and returns a string S which concatenates
% array elements with comma. C can be a cell array of strings, a character
% array, a numeric array, or a logical array. If C is a matrix, it is first
% flattened to get an array and concateneted. S = strjoin(C, separator) also
% specifies separator for string concatenation. The default separator is comma.
%
% Examples
%
% >> str = strjoin({'this','is','a','cell','array'})
% str =
% this,is,a,cell,array
%
% >> str = strjoin([1,2,2],'_')
% str =
% 1_2_2
%
% >> str = strjoin({1,2,2,'string'},'\t')
% str =
% 1 2 2 string
%
% >> str = strjoin({{'a' 1} {'b' 2}}, {'\n' ': '})
% str =
% a: 1
% b: 2
%

if nargin < 2, separator = ','; end
if iscell(separator)
for i = 1:length(separator)
assert(ischar(separator{i}), 'Invalid separator input at index %d: %s ', i, class(separator{i}));
end
else
output = strjoin(input, {separator});
return
end

output = '';
if ~isempty(input)
if ischar(input)
input = cellstr(input);
end
if isnumeric(input) || islogical(input)
output = [repmat(sprintf(['%.15g', separator{1}], input(1:end-1)), ...
1, ~isscalar(input)), ...
sprintf('%.15g', input(end))];
elseif iscellstr(input)
output = [repmat(sprintf(['%s', separator{1}], input{1:end-1}), ...
1, ~isscalar(input)), ...
sprintf('%s', input{end})];
elseif iscell(input)
if length(separator) < 2
separator{2} = separator{1};
end
output = strjoin(cellfun(@(x)strjoin(x, separator(2:end)), input, ...
'UniformOutput', false), ...
separator{1});
else
error('strjoin:invalidInput', 'Unsupported input: %s', class(input));
end
end
end

07 Apr 2014 Julian Hapke

Thanks! It's even faster than the newly introduced build-in strjoin (at least the 2013a-Version I tested with a 1x100000 cell array of strings)

Example:

>> a=cell(1,100000);[a{:}] = deal('bla');
>> b=nan;tic;for ii =1:10;b=strjoin(a,' ');end;toc;
Elapsed time is 5.018935 seconds.
>> b=nan;tic;for ii =1:10;b=strjoin2(a,' ');end;toc;
Elapsed time is 0.424864 seconds.

where strjoin2 is the one I'm commenting on

21 Mar 2013 Ying Yin

Thank you, Kota! I was about to write one myself too because I'm used to the join method in Ruby, but found yours. Nicely done!

Comment only
21 Mar 2013 Ying Yin  
21 Mar 2013 Ying Yin  
25 Jul 2012 Robert Moss

Very nicely done! I was about to write this myself, but this has saved me the effort. I like the way it handles arrays and cellstr arrays.

I'm curious: do you happen to be an IDL programmer? There is an IDL function of the same name with the "same" functionality.

Updates
22 Apr 2013 1.1

Added BSD licence statement; Improved efficiency;

Contact us