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

6 views (last 30 days)
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
Srishti Saha
Srishti Saha on 7 May 2018
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

Sign in to comment.

Accepted Answer

Stephen23 on 7 May 2018
function out = myjoin(varargin);
out = sprintf(' %s',varargin{:});
out = out(2:end);
And tested:
>> myjoin()
ans =
>> myjoin('hello','world')
ans = hello world

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!