askvity

How to use append in MATLAB?

Published in MATLAB Text Manipulation 5 mins read

To use append in MATLAB, you combine multiple text inputs into a single output string or string array.

The append function is a convenient way to concatenate text in MATLAB, especially when working with different types of text data like string arrays, character vectors, and cell arrays of character vectors. As stated in the reference, str = append( str1,...,strN ) combines the text from str1,...,strN.

Understanding the append Syntax

The basic syntax for the append function is:

str = append( str1,...,strN )

Here's a breakdown of the syntax elements:

  • str: The output variable that stores the combined text.
  • append: The function name.
  • str1,...,strN: One or more input arguments containing the text you want to combine.

Input Arguments (str1,...,strN)

According to the reference, each input argument can be:

  • A string array
  • A character vector
  • A cell array of character vectors

You can mix and match these input types within a single append call.

Output Argument (str)

The type of the output str depends on the input types:

  • If any input argument (str1 through strN) is a string array, then the output str is a string array.
  • If all inputs are character vectors or cell arrays of character vectors, the output is typically a string (if inputs are scalars) or a string array (if inputs have compatible sizes for element-wise operation).

How append Works

The append function concatenates the text from the input arguments.

  • Scalar Inputs: If all inputs are scalar (e.g., single character vectors or single-element string arrays), append simply joins the text together.
  • Array Inputs: If inputs are arrays (string arrays or cell arrays of character vectors), append performs an element-wise concatenation. This means the first element of each input is combined, then the second elements, and so on. Inputs must have compatible dimensions for element-wise operation.

Practical Examples of Using append

Let's look at some examples to illustrate how append handles different input types.

Example 1: Appending Character Vectors

str1 = 'Hello';
str2 = ' ';
str3 = 'World!';
combinedStr = append(str1, str2, str3);
disp(combinedStr);
  • Explanation: Combines three character vectors. The output combinedStr will be the string "Hello World!".

Example 2: Appending a String Array and Character Vectors

names = ["Alice"; "Bob"; "Charlie"]; % A string array
greeting = "Hi, ";                  % A scalar string (can be treated like a character vector in append context)
punctuation = '!';                   % A character vector

% Append element-wise
fullGreetings = append(greeting, names, punctuation);
disp(fullGreetings);
  • Explanation: Here, greeting is a scalar string and punctuation is a character vector. names is a 3x1 string array. append combines "Hi, " with each name in names, and then adds "!" to each result. Since names is a string array, the output fullGreetings will be a 3x1 string array: ["Hi, Alice!"; "Hi, Bob!"; "Hi, Charlie!"]. This demonstrates the rule: If any input is a string array, then the output is a string array.

Example 3: Appending Cell Arrays of Character Vectors

part1 = {'apple', 'banana'};      % Cell array
part2 = {' pie', ' split'};        % Cell array

desserts = append(part1, part2);
disp(desserts);
  • Explanation: append performs element-wise concatenation for cell arrays. The output desserts will be a 1x2 string array: ["apple pie" "banana split"]. Note that even though inputs were cell arrays, the output is a string array in recent MATLAB versions when used this way.

Example 4: Mixing String Arrays and Character Vectors

prefixes = ["Item "; "ID "];      % String array
values = [1; 10];                 % Numeric array
suffixes = [" - OK"; " - Error"];  % String array

% Convert numbers to strings for concatenation
valuesStr = string(values);

% Append the string arrays and the converted numeric string array
combinedData = append(prefixes, valuesStr, suffixes);
disp(combinedData);
  • Explanation: We convert the numeric array values to a string array valuesStr first, as append works with text. append then combines the corresponding elements from prefixes, valuesStr, and suffixes. Since all inputs are now string arrays (or compatible scalar strings/chars), the output combinedData is a 2x1 string array: ["Item 1 - OK"; "ID 10 - Error"].

Summary Table: append Inputs and Output

Input Type(s) Output Type Behavior
Character Vectors Only (Scalar) String Joins all inputs
Character Vectors Only (Mixed Sizes) String Array Element-wise (with scalar expansion)
Cell Arrays of Char Vectors Only (Mixed Sizes) String Array Element-wise (with scalar expansion)
Any input is a String Array (with compatible sizes) String Array Element-wise (with scalar expansion)
Any input is a String Array (scalar) String Joins all inputs

Note: Scalar expansion means a scalar input is repeated to match the dimensions of non-scalar inputs during element-wise operations.

Using append is generally preferred over the [ ] concatenation operator or functions like strcat for combining different types of text, especially when working with the modern string array type. It provides more consistent behavior with mixed input types and array operations.

Related Articles