1/16
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai |
|---|
No analytics yet
Send a link to your students to track their progress
character arrays (char data type):
a 1 by n character array is also known as a vector array
string arrays (string data type):
a m by n string array is also known as a string
a 1 by 1 string array is also known as a string scalar
cell arrays (cell data type)
what are the different types of text data types
enclosed by single quotes
e.g. message = ‘Hello’
each character is stored as an element in an array (above example has 5 elements)
can be created using the input function
» name = input (‘Please enter your name:’ ‘,’ ‘s’)
→ Please enter your name: (name)
name = (name)
how do we use character vectors
char
num2str
int2str
what functions do we use to convert numeric codes into characters
double or +0
what function do we use to convert characters to numeric code values
use round brackets
how do we access elements
use square brackets
how do we concatenate
newStr = lower(str)
newStr = upper(str)
where str is the character vector we want to convert
newStr is the transformed character vector
how do we change the case of our text
use == with caution
generally avoid when working with character vectors
→ generates an error if character vectors aren’t the same length
→ for equal length vectors produces a logical array (not a single logical value)
→ behaves differently when using strings
comparing text data
tf = strcmp (s1, s2)
where s1 is a character vector
s2 is a character vector
tf is a logical value indicating whether the character vectors are identical (logical 1) or not identical (logical 0)
tf = strncmp (s1, s2, n)
where n is an integer indicating the first n characters to compare
tf = strncmpi (s1,s2,)
→ ignores capitalisation
how do we use the strcmp function to compare character vectors
s = sprintf (format, data 1, …, data N)
where format is a character vector with format specifiers defining how to write the data
data 1, …, data N are the values or variables containing data to write out
s is a formatted character vector with each format specifier replaced by the formatted data
how do we format text data
s → character vector or string array
c → single character
d or i → decimal integer
e → scientific notation
f → decimal floating point
g → the shorter of e or f
what are the character conversions
\n
what character do we use to move to a new line
data = sscanf (s, format, size)
s is the text data to scan
format is a character vector detailing the data format we wish to read
size (optional), read enough data to fill an array with size cfInsizeA
data is the data extracted as a column vector
sscanf
k = strfind (str, pat)
str is a character vector to search in
pat is a character vector find in str
k is a numeric array that contains the starting index of each occurrence of pat in str
strfind
use double quotes
e.g. str_message = “Hello World”
contain a single string element
think of it as a container that holds a sequence of characters
you can extract the character vector from the container using curly braces {}
will work with the string comparison and formatting functions
string scalars
size and length
a string scalar is a container that holds one character vector
converting string scalars to numeric codes
If the string scalar is a text representation of a number, the double function will convert the string scalar into a numerical value:
>> double("100") = 100
To obtain the ASCII codes of the text stored in a string scalar, we need to access the character vector or convert it with the char function:
>> S = "Hello"; >> double(S{1}) = 72 101 108 108 111
>> S = "World"; >> double(char(S)) = 87 111 114 108 100
accessing elements
To access one or more string scalar elements, we first we must first access the character vector (i.e., with brace {} operators) before using standard 1D array indexing with parenthesis () operators.
concatenation
to concatenate string scalars together, we use the addition + operator
alternatively, we can use the strcat function with string scalars
however, if we use the strcat function with character vectors, it will strip trailing whitespace characters
comparing string scalars
recall that if we use the equality == operator to compare character vectors, MATLAB will throw an error when the character vectors have different lengths
this error does not occur when we compare string scalars with the equality == operator
what are the difference between string scalars and character vectors
use the {} operators, e.g. myCellArray = {'hello', "world", [1 6]; 5, "Peter", "Bier"};
how do we create cell arrays