This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.


Formatting a string

MuPAD® notebooks are not recommended. Use MATLAB® live scripts instead.

MATLAB live scripts support most MuPAD functionality, though there are some differences. For more information, see Convert MuPAD Notebooks to MATLAB Live Scripts.


stringlib::format(string1, width, <Left | Center | Right>, <fill_char>)


stringlib::format adjusts the length of a string.

If width is less than the length of the given string string1, the substring consisting of the first width characters of string1 is returned.

If width exceeds the length of string1, the given string will be filled with the necessary number of spaces or the optional fill_char. These are inserted at the end in case of left alignment, or at the beginning in case of right alignment. In case of centering, the same number of filling characters is placed at the beginning and at the end, but one more is placed at the end if their total number is odd.

If alignment is not given, left alignment is used by default.


Example 1

By default, a string of length 5 is adjusted to length 10 by inserting five space characters at the end. Since white spaces are collapsed in typesetting output, we use print:

print(Plain, stringlib::format("abcde", 10))
"abcde     "

In the case of centering, three spaces are inserted at the end and two at the beginning.

print(Plain, stringlib::format("abcde", 10, Center))
"  abcde   "

Instead of the space character, also any other character may be used as a filling character.

stringlib::format("abcde", 10, Right, ".")

stringlib::format("abcde", 10, ".")





Integer that determines the length of the returned string


One-character string to fill up the result string



Determines that the string will be aligned left


Determines that the string will be centered


Determines that the string will be aligned right

Return Values

String of length width containing the given string

See Also

MuPAD Functions

Was this topic helpful?