Concatenating strings

Problem

You want to put two or more strings together to make another string.

Solution

Strings are just vectors, so they can be combined the same way you normally combine vectors: by putting them in square brackets.

>> ['one'  ' two']
ans = one two

str = 'two';
['one '  str  ' three']
% one two three

Another way to do it is to use the sprintf function. This can be more readable than the method above.

In this example, each %s gets replaced with the variables after the main string.

str1 = 'two';
str2 = 'four';
sprintf('one %s three %s', str1, str2)
% one two three four