1/14
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
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
strcmp
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