Professional Documents
Culture Documents
Strings and File I/O: Programming With Matlab
Strings and File I/O: Programming With Matlab
Unit What
1 Introduction to MATLAB
2 Flow control & Data Structures
3 Plotting
4 Strings & File IO
5 Complex Numbers
6 Combinatorics
7 Linear Algebra
8 Statistics & Data Analysis
9 Polynomial Approximation,
Curve Fitting
10 Root Finding, Numerical
Differentiation
11 Numerical Integration
12 MATLAB GUI
2
CHARACTERS
>> whos
Name Size Bytes Class Attributes
s1 1x1 2 char
s2 1x1 2 char
3
CHARACTERS
• Unlike C++, MATLAB is not a strongly-typed language. This means that you can
assign to an operator a value of a different type
• Since characters have this numeric representation, many of MATLAB’s operators
are valid with characters.
• This sort of freedom may yield unexpected results, since it’s not intuitively obvious
what will occur in these situations.
• It’s generally unnecessary to operate on characters in this manner; proceed with
caution.
4
STRINGS
• A string is simply an array (row vector) of numeric codes that represent each of the
characters.
• As a result, the general rules and syntax for matrices apply here.
• For example, in this case we may concatenate horizontally:
• However, vertical concatenation [s1; s2] will yield an error because s1 is 1x7 and s2
is 1x6.
• If it’s necessary to store many strings into a matrix format, cell arrays are the best option:
{s1; s2}.
5
STRINGS
• Note that the == operator produces a logical array instead of a boolean output. This
can have undesired consequences when testing the equality of two strings.
• To check equality, use the strcmp function:
6
QUIZ 1
A. Syntax Error
B. 1
C. 126
D. 12600
E. ‘aaa’
7
QUIZ 1
A. Syntax Error
B. 1
C. 126
D. 12600
E. ‘aaa’
8
QUIZ 2
A. Syntax Error
B. P='rvrk'
C. P='ffrfvfrk'
D. P=‘aafdfrk'
E. P='aada'
9
QUIZ 2
A. Syntax Error
B. P='rvrk'
C. P='ffrfvfrk'
D. P=‘aafdfrk'
E. P='aada'
10
QUIZ 3
11
QUIZ 3
% Pro Solution
S2=S1(length(S1):-1:1);
% Amateur solution
for ii=1:length(S1)
S2(length(S1)+1-ii)=S1(ii)
end
12
FPRINTF VS. SPRINTF
• FPRINTF and SPRINTF are String processing libraries, common to very many
environments, scripting languages, and programming languages
• They go all the way back to FORTRAN, COBOL, BASIC (1960) and then C (1970), Java
(1990) and any scripting language (Python, TCL, Perl , ….)
• FPRINTF stands for «Print Formatted on file»
• SPRINTF stands for «Print Formatted on String»
• Many languages also have PRINTF, that writes on screen
• In MATLAB fprintf writes on screen if no file is specified
• FPRINTF/SPRINTF are high level commands based on the same subset of routines. Only
the top level interface (or API, Application Program Interface) is different
• They are used to produce a «Formatted» string starting from many different data
types, that are processed to fit in the string format depending on the user directives
• The only difference is that fprintf writes on file or screen, sprintf on a string variable:
fprintf(‘I like the letter E’)
s=sprintf(‘I like the letter E’); disp(s);
13
FORMATTING DATA INTO STRINGS
• Data is placed inside the string sequentially, starting with the integer ii that
represents the index of the array data.
• We place this output into the string using the % symbol followed by the conversion
character d, which formats output ii into a signed integer.
• The second output data(ii) corresponds to %g in our format string, which
formats floating-point outputs in a compact manner.
14
• \n is a special newline character.
QUIZ 4
15
QUIZ 4
16
QUIZ 5
dude(3).name='maude';
dude(3).gender='f';
dude(3).birth=1960;
s= sprintf(‘%s was born in %d \n',dude[3].name,
dude[3].birth);
17
QUIZ 5
dude(3).name='maude';
dude(3).gender='f';
dude(3).birth=1960;
s= sprintf(‘%s was born in %d \n',dude[3].name, dude[3].birth);
SYNTAX ERROR! dude[3] should be dude(3), as square brackets are used to define
arrays, round brackets to index arrays.
If we fix the error, then
18
FORMATTING OPERATORS
• When formatting floating-point data into a string, it may be necessary to alter the
notation or number of decimal places.
• The conversion characters %f and %e format data to fixed-point and exponential
notation, respectively.
• The field width and precision can be modified for these formats using the syntax:
%<field width>.<precision>f.
ans =
314.16
314.15927
2.090000e-008
2.09e-008
19
REGULAR EXPRESSIONS
• If only starting indices or end indices of the matching substrings are desired, the
outselect argument can be omitted:
20
REGULAR EXPRESSIONS
• We wish to search str for all instances of the word “color” and its derivatives, e.g.,
“colorful”, without discriminating between British and American spelling
(color/colour).
21
REGULAR EXPRESSIONS
• Useful metacharacters:
• [c1c2c3] – any character contained within the brackets, e.g., '[kf#]ind'
matches 'kind', 'find', or '#ind'.
• [^c1c2c3] – any character not contained within the brackets, e.g.,
'[^kf]ind' can match 'mind' or '#ind' but not 'kind' or 'find'.
• \w – any alphabetic, numeric or underscore character.
• \W – any character that is not alphabetic, numeric, or underscore.
• \s – any whitespace character.
• . – any single character, including whitespace.
• Useful quantifiers:
• expr* - match 0 or more times consecutively.
• expr? – match 0 times or 1 time.
• expr{m,n} – match at least m times, but no more than n times consecutively,
e.g., '\w{3,6}ing[\s,.]' matches all 6 to 9 letter words that end in “ing”
followed by a whitespace , comma, or period
22
REGULAR EXPRESSIONS
23
REGULAR EXPRESSIONS
• To replace the text within a string that matches the desired regular expression use
the regexprep function.
• The parts of str that match expr are replaced by the string replace and the
resulting string is outputted in newStr.
• For example, replace all instances of “color” with the British spelling “colour”:
24
FILE I/O
Work
MATLAB
Space
• MATLAB Variables are handled by the tool as part of its internal database. It may in the
computer memory, or it may be swapped to the hard disk but that is all handled by
MATLAB, that keeps those variable easily IMPLICITLY accessible for us at any time.
fileID=fopen(‘File’,…);
[…] File
fread/fwrite(fileID,…); MATLAB
[…]
System
fclose(fileID);
25
LOW LEVEL I/O
26
LOW LEVEL I/O (TEXT FILES)
• You can save files in Binary form, but it may make sense to use TEXT for so that they
are human-readable. The only drawback of using text files is the SIZE of the file itself
• format specifies the type of field to read with specifies similar to the sprintf
function, e.g., %d reads in signed integers, %f reads in floating-point numbers.
• To skip fields, insert an * after the %, e.g., %*d skips integers.
• To skip a set of specific characters insert them in format, e.g., to only read the floating
point number in 'sin(45) = 0.7071', specify a format of 'sin(45) = %f'.
27
EXAMPLE: TEXT FILES
Trajectory Data
numData = 5
• We are aware of the file’s formatting ahead of time and we wish to read in all
numerical data contained within.
• First, open the file for reading in text mode and store the file identifier:
28
EXAMPLE: TEXT FILES
• Skip through the first two strings by specifying %*s twice and move to the next line
with special character \n.
• Specify the literal characters to skip 'numData = '.
• Data is only read when into N once the specifier %d is reached.
• N is a scalar; only one integer is read in.
29
EXAMPLE: TEXT FILES
numData = 5
• The file pointer is positioned after the last element read, which is currently after 5.
• The next task is to advance the pointer so that it is positioned right before 1.00.
30
QUIZ 6
• Write the scanf operation that is capable of reading the data array above
31
QUIZ 6
• Write the scanf operation that is capable of reading the data array above
Note: A will create an empty variable, as you keep rewriting on data but only the first round is significant.
The formats are all legal, even though C/D are preferable as more readable. Any solution without the [3
N] will create a single vector, rather than a matrix. The ' after round brackets in solution D is necessary
to transpose data, as values are stored in data by columns
32
EXAMPLE: TEXT FILES
• Since the floating-point elements are read in column order, transpose data.
• Close the file after finishing.
33
LOW LEVEL I/O (BINARY FILES)
• The low-level file I/O functions fread and fwrite offer the most control over
reading and writing data to files.
• These functions operate directly at the bit-level.
• For example, we wish to read the first 10 bytes of our example_data.txt:
bytesData =
84 114 97 106 101 99 116 111 114 121
>> uint8('Trajectory')
ans =
84 114 97 106 101 99 116 111 114 121
34
EXCEL FILES
• Excel file are such a common format to describe dataset that MATLAB offers a specific
set of commands for them
• To import data from an Excel spreadsheet, use the xslread function:
• This reads data from the specified worksheet, sheet, and range, xlRange.
• Numeric data is returned in num, text fields are returned in cell array txt, and
unprocessed data (numbers and text) is returned in cell array raw.
• To export data to an excel spreadsheet, use the xslwrite function:
• Writes the data in array A to the specified worksheet, sheet, and range, xlRange, of
Excel file filename.
• The array A can be a cell array if text data or mixed data (text and numbers) needs to
be written.
• Can only be a cell array of text and numbers. NOT A CELL ARRAY OF CELL ARRAYS!
35
EXAMPLE: EXCEL FILES
• Assume we have the data from the previous example formatted into a spreadsheet
called exampleData.xlsx:
• We wish to read in the data of the object’s position and calculate its distance from
the origin at each time.
• Afterwards, we wish to record this distance in the column to the right of “YPos”.
36
EXAMPLE: EXCEL FILES
• The desired data, XPos and YPos, lies in columns B to C and rows 5 to 9, i.e.,
range B5:C9.
• The data is on worksheet 1, so this time we may omit the sheet parameter.
• We only want to retrieve numerical data, so we only need to specify the first output.
pos =
16.2900 3.1500
18.1200 19.4100
2.5400 19.1400
18.2600 9.7100
12.6400 16.0000
37
EXCEL FILES
• First, we need to label the new column we want to write containing distance data.
• Specify strings as a cell array, otherwise all characters are written in separate,
adjacent cells.
• The column label is placed in cell D4 and the data vector will begin in cell D5.
dist =
16.5918
26.5534
19.3078
20.6812
20.3904
38
IMAGES
I = imread(filename)
39
IMAGES
• Set all red and blue values to 0 and write the result to an output image:
>> img(:,:,1) = 0;
>> img(:,:,3) = 0;
>> imwrite(img, 'footballGreen.jpg')
>> figure, imshow(img) %display result
40
VIDEOS
• Videos can be loaded to be played directly in MATLAB using the implay function.
implay('xylophone.mp4')
videoObj = VideoReader('xylophone.mp4');
frame = read(videoObj, 130); %read frame 130
41
SPECIFYING FILEPATHS
• When specifying a simple filename to a MATLAB function, the script that contains the
call to the function requires that the file be placed in script’s directory.
• It can be cumbersome and inefficient to place input files directly inside your current
folder.
• Use sprintf to create filenames that include their location:
filename = 'example.txt';
path = sprintf('C:\\Users\\Fener\\Desktop\\MatlabDrs\\EEE225\\
inputs\\%s', filename);
C:\Users\Fener\Desktop\MatlabDrs\EEE225\inputs\example.txt
42