0
to 9
are stored as \(48\) to \(57\). A
to Z
are stored as \(65\) to \(90\). a
to z
are stored as \(97\) to \(122\). char
variable type in MATLAB. 'Hello World!'
is a string, and char([72 101 108 108 111 32 87 111 114 108 100 33])
represents the same string. ['Hello ', 'World' '!']
is the same as 'Hello World!'
. append(x, y, ...)
also combines the strings \(x, y, ...\), for example, append('Hello ', 'World', '!')
returns 'Hello World!'
. strcat(x, y, ...)
combines (or conCATenate) the strings \(x, y, ...\) and removes trailing spaces, for example, strcat('Hello ', 'World', '!')
returns 'HelloWorld!'
. 'abc' + 1
'abc1'
'bcd'
text = 'a':'e'; text(end:-1:1)
'dcb'
'edcba'
num2str(x, n)
converts a number \(x\) (not ASCII code) to a string, rounded to \(n\) significant digits (different from \(n\) decimal places), for example, num2str(pi, 4)
is the same as 3.142
or char([51 46 49 52 50])
. str2num(x)
converts a string back to a number or a matrix, for example, str2num('3.142')
returns the number \(3.142\) and str2num('1 2; 3 4')
returns the matrix \(\begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}\). str2num('1') + num2str(1)
'11'
'50'
string
variable type that stores multiple characters as a single object so that multiple strings can be stored in a vector without getting combined into one, for example, ['a' 'b' 'c']
is the same as 'abc'
but ["a" "b" "c"]
stays a vector and "a" + "b" + "c"
is the same as "abc"
. string('abc')
becomes "abc"
and char("abc")
becomes 'abc'
. strfind(x, y)
finds the indices of all occurrences of \(y\) in \(x\), for example, strfind('aabb', 'a')
returns \(\begin{bmatrix} 1 & 2 \end{bmatrix}\). strrep(x, o, n)
or replace(x, o, n)
replaces all occurrences of \(o\) in \(x\) by \(n\) and returns the new string, for example, strrep('aabb', 'b', 'c')
returns 'aacc'
and replace('aabb', ["a", "b"], ["c", "d"])
return 'ccdd'
. blanks(n)
creates a string with \(n\) spaces. ''
(two single quotation marks, note one double quotation mark) is '
. %%
is %
. \\
is backslash \
. \n
is new line. \t
is tab. disp(x)
displays the string \(x\). It does not store \(x\) in the variable ans
. fprintf(x, v1, v2, ...)
displays a string with %s
(string), %i
(integer), %f
(floating point), %e
(scientific notation) replaces by \(v_{1}, v_{2}, ...\). %
to set the field width (text length) for the string, for example, %5s
and %-5i
make sure that the displayed string has length \(\geq 5\) by adding spaces when necessary. A positive number means added spaces are on the left and a negative number means added spaces are on the right. .
followed by a number for %f
to set the precision, the number of digits after the decimal point, for example, %.4f
rounds the number to \(4\) decimal places, adding \(0\)s when necessary. fprintf('a%-2ib', 1)
'a1b'
'a1 b'
'a 1b'
'a-1b'
fprintf('a%5.2fb', pi)
'a3.14b'
'a 3.14b'
'a3.14 b'
'a3.142b'
input(x)
gets a user input in MATLAB syntax. String \(x\) is the prompt. input(x, 's')
gets a user input as a string. menu(x, c1, c2, ...)
or listdlg('ListString', c1, c2, ..., 'PromptString', x)
gets a user input from a list of choices \(c_{1}, c_{2}, ...\), and returns the index. length(input('Enter a string', 's'))
and user enters '10'
(with the quotes). input('Enter x1', 's') + input('Enter x2')
and user enters 10
and 1
. '101'
'11'
opt = ["one", "two", "three"]; opt(menu('Select a string', '1', '2', '3'))
and user selects 2
. 'two'
'2'
'n'
load(x, '-ascii')
loads the text file with name \(x\). load(x)
can load a .mat
binary file. readmatrix(x)
loads the text or spreadsheet file with name \(x\) into a single matrix. fileread(x)
reads the text file with name \(x\) as a string with char
type. readlines(x)
reads the text file with name \(x\) as a vector of lines, each line has the string
type. save(x, v, ..., '-ascii')
saves the variables with names \(v, ...\) to the file with name \(x\). save(x, v, ...)
saves the variables in a .mat
binary file, not human-readable. writematrix(v, x)
saves the variable \(v\) to the file with name \(x\). fopen(x, 'w'); fprintf(x, v); fclose(x);
writes the string \(v\) to the file with name \(x\). Last Updated: March 03, 2025 at 12:52 AM