File Exchange

image thumbnail

STRJOIN

version 1.1 (2.61 KB) by

Concatenate an array into a single string.

17 Downloads

Updated

View License

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

Comments and Ratings (6)

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

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

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!

Ying Yin

Ying Yin

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

1.1

Added BSD licence statement; Improved efficiency;

MATLAB Release
MATLAB 7.12 (R2011a)

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

» Watch video