| Products & Services | Solutions | Academia | Support | User Community | Company |
| Download Product Updates | | | Get Pricing | | | Trial Software |
| Documentation → MATLAB |
| Contents | Index |
| Learn more about MATLAB |
| On this page… |
|---|
Creating Strings with Concatenation Comparing Methods of Concatenation Store Arrays of Strings in a Cell Array |
Strings are often created by concatenating smaller elements together (e.g., strings, values, etc.). Two common methods of concatenating are to use the MATLAB concatenation operator ([]) or the sprintf function. The second and third line below illustrate both of these methods. Both lines give the same result:
numChars = 28;
s = ['There are ' int2str(numChars) ' characters here']
s = sprintf('There are %d characters here', numChars)For more information: See Creating Character Arrays and Converting from Numeric to String in the MATLAB Programming Fundamentals documentation.
When building strings with concatenation, sprintf is often preferable to [] because
It is easier to read, especially when forming complicated expressions
It gives you more control over the output format
It often executes more quickly
You can also concatenate using the strcat function, However, for simple concatenations, sprintf and [] are faster.
It is usually best to store an array of strings in a cell array instead of a character array, especially if the strings are of different lengths. Strings in a character array must be of equal length, which often requires padding the strings with blanks. This is not necessary when using a cell array of strings that has no such requirement.
The cellRecord below does not require padding the strings with spaces:
cellRecord = {'Allison Jones'; 'Development'; 'Phoenix'};For more information: See Cell Arrays of Strings in the MATLAB Programming Fundamentals documentation.
You can convert between standard character arrays and cell arrays of strings using the cellstr and char functions:
charRecord = ['Allison Jones'; 'Development '; ...
'Phoenix '];
cellRecord = cellstr(charRecord);
Also, a number of the MATLAB string operations can be used with either character arrays, or cell arrays, or both:
cellRecord2 = {'Brian Lewis'; 'Development'; 'Albuquerque'};
strcmp(charRecord, cellRecord2)
ans =
0
1
0For more information: See Converting to a Cell Array of Strings and String Comparisons in the MATLAB Programming Fundamentals documentation.
Using regular expressions in MATLAB offers a very versatile way of searching for and replacing characters or phrases within a string. See the help on these functions for more information.
Function | Description |
|---|---|
Match regular expression. | |
Match regular expression, ignoring case. | |
Replace string using regular expression. |
For more information: See Regular Expressions in the MATLAB Programming Fundamentals documentation.
![]() | Variables | Evaluating Expressions | ![]() |

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.
| © 1984-2009- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |