File Exchange

image thumbnail

CStr2String

version 1.2.0.0 (8.46 KB) by Jan
Fast concatenation of cell strings to a string as C-Mex

1 Download

Updated 28 Nov 2010

View License

Concatenate strings of a cell string

This equals CAT(2, C{:}) and SPRINTF('%s', C{:}), but is remarkably faster,
because the output is pre-allocated.

Str = CStr2String(CStr, Separator, Trail)
INPUT:
CStr: Cell string of any size. All not-empty cell elements must be
strings ([1 x N] CHAR vectors).
Separator: String, which is appended after each string of CStr.
This is thought to emulate: "sprintf(['%s', Sep], CStr{:})".
Optional, default: ''.
Trail: String or logical flag. For 'noTrail' or FALSE the trailing
separator is omitted. Optional, default: 'Trail'.

OUTPUT:
Str: [1 x M] CHAR vector, concatenated strings of the input.

EXAMPLES:
Write a cell string to a file:
Slow: fprintf(FID, '%s\n', CStr{:});
Fast: fwrite(FID, CStr2String(CStr, char(10)), 'uchar');
A comma-separated list;
CStr = {'First', 'Second', 'Third'});
Str = CStr2String(CStr, ', ', 'noTrail');
>> 'First, Second, Third'

COMPILATION:
mex -O CStr2String.c
or download from: http://www.n-simon.de/mex
Please run the unit-test uTest_CStr2String after compiling!

Tested: Matlab 6.5, 7.7, 7.8, WinXP, 32bit
Compiler: LCC2.4/3.8, BCC5.5, OWC1.8, MSVC2008
Assumed Compatibility: higher Matlab versions, Mac, Linux, 64bit

See also: HORZCAT, CAT, SPRINTF, STRCAT.
FEX: http://www.mathworks.com/matlabcentral/fileexchange/28916-cell2vec

Cite As

Jan (2019). CStr2String (https://www.mathworks.com/matlabcentral/fileexchange/26077-cstr2string), MATLAB Central File Exchange. Retrieved .

Comments and Ratings (9)

Jan

@claudio: What does "crash" mean? Do you get an error message? I expect the message: "1st Input must be a cell string.", because your "data" is a char vector and not a cell string.

claudio

hi Jan

with Matlab R2014a, the function execution crashes Matlab. Exactly with the string

data = '2018-02-16;' % char
CStr2String(data)

what happens? is it my mistake?
thanks...

Very slick. This is orders of magnitude faster than any matlab alternative I could find.

Bruno Luong

MATLAB cell concatenation is extremely slow. Jan's FEX is a must for those who care about speed.

Jan

The test data for the determination of speed have been unrealistic! I've used: C = cell(1, 1000); C(:)={blanks(n)}; This creates strings with shared data, which is an advantage for CStr2String and a disadvantage for CAT and SPRINTF. Sorry!
For realistic cell strings, e.g. 100 strings of length 10, CStr2String is 3 (not 4.1) times faster than CAT. For 10.000 strings of length 10 the speed gain is about 6.5 (not 61) (Matlab 7.8, WinXP). At least CStr2String is faster...
An updated test function and screen shot is coming soon!

Jan

While there is no significant speed difference between CAT(2, C{:}) and [C{:}] in Matlab 7.8, in Matlab 6.5 the later is 4% faster at least for short {1 x 5} cells - but still much slower than CStr2String.
Internally [C{:}] calls HORZCAT (asked PROFILE with -detail builtin), so HORZCAT is not an alternative also.
Thanks Jos for pointing this out.

Jos (10584)

and what about the simple [C{:}] ?

Updates

1.2.0.0

3rd input to suppress trailing separator

1.1.0.0

The test function uses more realistic test data now. This reduces the displayed speed-up.

MATLAB Release Compatibility
Created with R2009a
Compatible with any release
Platform Compatibility
Windows macOS Linux