pad
Adds leading or trailing spaces to text.
Category: Language Fundamentals > Data Types
Syntax
newStr = pad(str)
Pads every element on the right to the length of the longest one.
newStr = pad(str, n)
Pads to at least n characters.
newStr = pad(str, side)
Pads on the side given: 'left', 'right' (default) or 'both'.
newStr = pad(str, n, side)
Combines both.
Description
pad only lengthens text; it never truncates. With no length argument it makes all elements of a string array the same width, which is handy for aligned output.
Input arguments
str- String array, character vector or cell array of character vectors.
n- Target length in characters.
side'left','right'or'both'.
Output arguments
newStr- Text of the same type as
str. Elements already long enough are unchanged.
Example
pad("abc", 6) + "|"
pad("abc", 6, 'left') + "|"
pad(["a", "bbb"]) + "|"
Output
ans = "abc |"
ans = " abc|"
ans = 1×2 string array
"a |" "bbb|"