How can I take variable number of input strings in a function and return the concatenated output string?

9 ビュー (過去 30 日間)
Srishti Saha
Srishti Saha 2018 年 5 月 7 日
コメント済み: Srishti Saha 2018 年 5 月 7 日
I have to write a function that takes variable number of inputs (even no input is allowed) and returns a concatenate string as the output.
Example: x1 = 'my'; x2= ' '; x3= 'holiday'; x4= ' '; x5= 'is'; x6= ' almost over!'; for all 6 input strings output would be= 'my holiday is almost over!';
if no input was passed, output should be empty
  2 件のコメント
Srishti Saha
Srishti Saha 2018 年 5 月 7 日
I have posted what I did. I just wanted to see if there is a better solution. you comment explains everything quite well. thanks.
This wasn't homework but a part of my end semester project

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

採用された回答

Stephen
Stephen 2018 年 5 月 7 日
function out = myjoin(varargin);
out = sprintf(' %s',varargin{:});
out = out(2:end);
end
And tested:
>> myjoin()
ans =
>> myjoin('hello','world')
ans = hello world
  3 件のコメント

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

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by