concatenating strings

3 views (last 30 days)
Christina
Christina on 15 Aug 2011
Hi all, I have two cell arrays containing dates and times e.g. D{1}='11/06/29' '11/06/30' etc
D{2}='10:36' '10:35' etc My aim is to concatenate the two strings with a space in between, i.e. to end up with '11/06/29 10:36' '11/06/30 10:35' etc Can someone help please? I have not managed to solve this, although it seems trivial. Many thanks, Christina

Accepted Answer

Titus Edelhofer
Titus Edelhofer on 15 Aug 2011
Hi Christina,
not too nice but working:
D{1}={'11/06/29' '11/06/30'};
D{2}={'10:36' '10:35'};
x = strcat(D{1}, '$', D{2});
x = strrep(x, '$', ' ');
Titus

More Answers (1)

Jan
Jan on 15 Aug 2011
The type and dimensions of the input is not clear:
"D{1}='11/06/29' '11/06/30'"
Does D{1} contain a cell string?! Then D would be a nested cell. Or is D a {n x 2} cell string? It is hard to create a meaningful answer, if the data structure is not defined exactly...
Usually STRCAT is the best choice to concatenate cell strings. Let me guess:
D1 = {'11/06/29', '11/06/30'}
D2 = {'10:36' '10:35'}
R = strcat(D1, {' '}, D2);
It is necessary to use a cell string for the space, because for strange historical reasons STRCAT removes marginal spaces of processed cell strings.
If speed matters, this function is 10 times faster, but needs to be compiled: FEX: CStrCatStr.
  1 Comment
Christina
Christina on 15 Aug 2011
sorry, my bad. The structure looked different in the preview but I failed to check. Thanks for the answer, anyway.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!