Prev: W3 Next: W5

📗 Tuesday lectures: 4:00 to 4:50, Zoom, TopHat: Link (or Google Form: Form if TopHat not working). MATLAB.
📗 Programming Homework: P2

Slide:


# ASCII Code

📗 ASCII stands for American Standard Code for Information Interchange: Link.
📗 Each character is stored as an integer (its ASCII code).
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\).
📗 A string is a list of characters.
📗 A string is stored as a (row) vector of integers with 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.

# Combining Strings

📗 Two strings can be combined the same way two vectors are combined, for example, ['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!'.
TopHat Quiz
📗 'abc' + 1
➩ A: 'abc1'
➩ B: 'bcd'
➩ C: \(\begin{bmatrix} 98 & 99 & 100 \end{bmatrix}\)
➩ D: \(\begin{bmatrix} 146 & 147 & 148 \end{bmatrix}\)

TopHat Quiz
📗 text = 'a':'e'; text(end:-1:1)
➩ A: 'dcb'
➩ B: \(\begin{bmatrix} 100 & 99 & 98 \end{bmatrix}\)
➩ C: 'edcba'
➩ D: \(\begin{bmatrix} 101 & 100 & 99 & 98 & 97 \end{bmatrix}\)
 

# String Conversion

📗 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}\).
TopHat Quiz
📗 str2num('1') + num2str(1)
➩ A: '11'
➩ B: \(11\)
➩ C: '50'
➩ D: \(50\)


# String vs Character Array

📗 In MATLAB, there is a 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".
📗 To convert between the two types of string, string('abc') becomes "abc" and char("abc") becomes 'abc'.

# Useful String Functions, Find and Replace

📗 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.

# Text Output

📗 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}, ...\).
➩ Add a number after % 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.
➩ Add a . 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.
TopHat Quiz
📗 fprintf('a%-2ib', 1)
➩ A: 'a1b'
➩ B: 'a1 b'
➩ C: 'a 1b'
➩ D: 'a-1b'

TopHat Quiz
📗 fprintf('a%5.2fb', pi)
➩ A: 'a3.14b'
➩ B: 'a 3.14b'
➩ C: 'a3.14 b'
➩ D: 'a3.142b'


# Text Input

📗 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.
TopHat Quiz
📗 length(input('Enter a string', 's')) and user enters '10' (with the quotes).
➩ A: \(2\)
➩ B: \(4\)
➩ C: \(6\)
➩ D: \(10\)

TopHat Quiz
📗 input('Enter x1', 's') + input('Enter x2') and user enters 10 and 1.
➩ A: '101'
➩ B: '11'
➩ C: \(11\)
➩ D: \(\begin{bmatrix} 50 & 49 \end{bmatrix}\)

TopHat Quiz
📗 opt = ["one", "two", "three"]; opt(menu('Select a string', '1', '2', '3')) and user selects 2.
➩ A: 'two'
➩ B: '2'
➩ C: \(2\)
➩ D: 'n'


# File Input

📗 Matrices can loaded from files into variables.
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.
➩ Under the "HOME" tab, there is a "Import Data" tool that can be used to import data in various formats from files.
📗 Text can be loaded from files into variables.
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.

# File Output

📗 Matrices can be written to files.
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\).
📗 Text can be written to files.
fopen(x, 'w'); fprintf(x, v); fclose(x); writes the string \(v\) to the file with name \(x\).


📗 Notes and code adapted from the course taught by Professors Beck Hasti and Michael O'Neill.
📗 You can expand all TopHat Quizzes and Discussions: .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google form Form at the end of the lecture.





Last Updated: March 03, 2025 at 12:52 AM