how to join 3 strings into single string?

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

 採用された回答

Stephen23
Stephen23 2017 年 4 月 2 日
編集済み: Stephen23 2017 年 4 月 2 日

0 投票

No ugly and inefficient loop is required:
>> C = {'Tcgh','A6','1214'};
>> out = sprintf('-%s',C{:});
>> out(2:end)
ans =
Tcgh-A6-1214

6 件のコメント

chocho
chocho 2017 年 4 月 2 日
@Stephen Cobeldick hi bro Stephen I really Appreciate your help
But my inputs are in cell array contain cell array so i got the folowing error :
Error using sprintf
Function is not defined for 'cell' inputs.
could you check the inputs I posted on 'cat.png'
Stephen23
Stephen23 2017 年 4 月 2 日
@chocho: I am happy to help you but I cannot load data from a screenshot. A screenshot is useless for giving me data. Do you want to me to waste my time by trying to replicate your data arrays? If you actually want help then please upload your data.
chocho
chocho 2017 年 4 月 2 日
@Stephen Cobeldick ok sure
Stephen23
Stephen23 2017 年 4 月 2 日
編集済み: Stephen23 2017 年 4 月 2 日
>> out = cellfun(@(c)sprintf('%s-%s-%s',c{:}),InterN,'Uni',0)
out =
'TCGA-A6-2677'
'TCGA-AA-3516'
'TCGA-AA-3520'
'TCGA-AA-3521'
'TCGA-AA-3522'
'TCGA-AA-3524'
'TCGA-AA-3525'
'TCGA-AA-3527'
... etc
And look how quickly I could actually help you, once you gave your data. The more you help us, the easier we can help you.
chocho
chocho 2017 年 4 月 2 日
Many Thanks @Stephen Cobeldick but i'm trying to avoid cellfun and prefer to use for loop for future use.
chocho
chocho 2017 年 4 月 2 日
@Stephen Cobeldick yes, you helped me a lot Thank you sooooo much

サインインしてコメントする。

その他の回答 (1 件)

Nicolaie Popescu-Bodorin
Nicolaie Popescu-Bodorin 2017 年 4 月 2 日

0 投票

res = strCell{1};
for k=2:length(strCell),
res=[res '-' strCell{k}];
end;
disp(res);

1 件のコメント

Stephen23
Stephen23 2017 年 4 月 2 日
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.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2017 年 4 月 2 日

コメント済み:

2017 年 4 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by