%% Text in String and Character Arrays
% There are two ways to represent text in MATLAB®. Starting in R2016b, you can store text in string arrays. And in any version of MATLAB, you can store text in character arrays. A typical use for character arrays is to store pieces of text as character vectors. MATLAB displays strings with double quotes and character vectors with single quotes.
%
%% Represent Text with String Arrays
% You can store any 1-by-n sequence of characters as a string, using the string data type. Starting in R2017a, enclose text in double quotes to create a string.

str = "Hello, world"

%
% Though the text "Hello, world" is 12 characters long, str itself is a 1-by-1 string, or string scalar. You can use a string scalar to specify a file name, plot label, or any other piece of textual information.
% To find the number of characters in a string, use the strlength function.

n = strlength(str)

%
% If the text includes double quotes, use two double quotes within the definition.

str = "They said, ""Welcome!"" and waved."

%
% To add text to the end of a string, use the plus operator, +. If a variable can be converted to a string, then plus converts it and appends it.

fahrenheit = 71;
celsius = (fahrenheit-32)/1.8;
tempText = "temperature is " + celsius + "C"

%
% Starting in R2019a, you can also concatenate text using the append function.

tempText2 = append("Today's ",tempText)

%
% The string function can convert different types of inputs, such as numeric, datetime, duration, and categorical values. For example, convert the output of pi to a string.

ps = string(pi)

%
% You can store multiple pieces of text in a string array. Each element of the array can contain a string having a different number of characters, without padding.

str = ["Mercury","Gemini","Apollo";...
       "Skylab","Skylab B","ISS"]

%
% str is a 2-by-3 string array. You can find the lengths of the strings with the strlength function.

N = strlength(str)

%
% As of R2018b, string arrays are supported throughout MATLAB and MathWorks® products. Functions that accept character arrays (and cell arrays of character vectors) as inputs also accept string arrays.
%
%% Represent Text with Character Vectors
% To store a 1-by-n sequence of characters as a character vector, using the char data type, enclose it in single quotes.

chr = 'Hello, world'

%
% The text 'Hello, world' is 12 characters long, and chr stores it as a 1-by-12 character vector.

whos chr

%
% If the text includes single quotes, use two single quotes within the definition.

chr = 'They said, ''Welcome!'' and waved.'

%
% Character vectors have two principal uses:
% To specify single pieces of text, such as file names and plot labels.
% To represent data that is encoded using characters. In such cases, you might need easy access to individual characters.
% For example, you can store a DNA sequence as a character vector.

seq = 'GCTAGAATCC';

%
% You can access individual characters or subsets of characters by indexing, just as you would index into a numeric array.

seq(4:6)

%
% Concatenate character vector with square brackets, just as you concatenate other types of arrays.

seq2 = [seq 'ATTAGAAACC']

%
% Starting in R2019a, you also can concatenate text using append. The append function is recommended because it treats string arrays, character vectors, and cell arrays of character vectors consistently.

seq2 = append(seq,'ATTAGAAACC')

%
% MATLAB functions that accept string arrays as inputs also accept character vectors and cell arrays of character vectors.
% Copyright 2012-2019 The MathWorks, Inc.