how to join 3 strings into single string?

5 vues (au cours des 30 derniers jours)
chocho
chocho le 2 Avr 2017
Commenté : chocho le 2 Avr 2017
I have a cell array [1*3 cell] Example Tcgh A6 1214 and i want to join them by put '-' in between and get Tcgh-A6-1214
i tried strcat and strjoin but doesn't work with me! Thanks

Réponse acceptée

Stephen23
Stephen23 le 2 Avr 2017
Modifié(e) : Stephen23 le 2 Avr 2017
No ugly and inefficient loop is required:
>> C = {'Tcgh','A6','1214'};
>> out = sprintf('-%s',C{:});
>> out(2:end)
ans =
Tcgh-A6-1214
  6 commentaires
chocho
chocho le 2 Avr 2017
Many Thanks @Stephen Cobeldick but i'm trying to avoid cellfun and prefer to use for loop for future use.
chocho
chocho le 2 Avr 2017
@Stephen Cobeldick yes, you helped me a lot Thank you sooooo much

Connectez-vous pour commenter.

Plus de réponses (1)

Nicolaie Popescu-Bodorin
Nicolaie Popescu-Bodorin le 2 Avr 2017
res = strCell{1};
for k=2:length(strCell),
res=[res '-' strCell{k}];
end;
disp(res);
  1 commentaire
Stephen23
Stephen23 le 2 Avr 2017
This expands the output res on each iteration, which is not efficient:
See my answer for a simpler and more efficient solution that does not use a loop.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by