concatenate cell
Ältere Kommentare anzeigen
i have a cell (symbol {} on the variable list) with a=('kkk', 'lll', 'xxx', 'jjj.xls')
and i want to concatenate a so that i can have
b = ('kkk_lll_xxx_jjj.xls')
thank you
Akzeptierte Antwort
Weitere Antworten (4)
Jan
am 20 Dez. 2011
a = {'kkk'; 'lll'; 'xxx'; 'jjj.xls'}
out = sprintf('%s_', a{:});
out(end) = [];
or:
out = [sprintf('%s_', a{1:end-1}), a{end}];
or:
out = sprintf([repmat('%s_', 1, numel(a)-1), '%s'], a{:});
For very large cell strings, this becomes slow because Matlab forgets to pre-allocate the output properly. Therefore I've written the C-mex function CStr2String:
out = CStr2String(a, '_', 'noTrial')
1 Kommentar
Andrei Bobrov
am 20 Dez. 2011
Hi Jan! +1.
Alexandros
am 20 Dez. 2011
0 Stimmen
Alexandros
am 20 Dez. 2011
0 Stimmen
Eric Tao
am 3 Feb. 2018
just type:
b = {strjoin(a,'_')};
Then your b will be a cell as
{'kkk_lll_xxx_jjj.xls'}
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!