In MATLAB, string interpolation is done using the sprintf
function. Here's an example:
main.m110 chars5 lines
Output:
main.m39 chars2 lines
In the example above, %s
is a placeholder for a string and %d
is a placeholder for a decimal integer. The values of name
and age
are passed to sprintf
as additional arguments after the format string.
You can also use the newer string interpolation syntax introduced in MATLAB R2016b using the dollar sign $
followed by parentheses:
main.m95 chars5 lines
Output:
main.m39 chars2 lines
In the example above, the variables name
and age
are enclosed in curly braces ({}
) inside the string. Note that this syntax is only available in MATLAB R2016b or later.
gistlibby LogSnag