Presentation is loading. Please wait.

Presentation is loading. Please wait.

AN ENGINEER’S GUIDE TO MATLAB

Similar presentations


Presentation on theme: "AN ENGINEER’S GUIDE TO MATLAB"— Presentation transcript:

1 AN ENGINEER’S GUIDE TO MATLAB
3rd Edition CHAPTER 3 DATA INPUT/OUTPUT Copyright © Edward B. Magrab 2009

2 Chapter 3 – Objectives Present the means of displaying annotated numerical results in the MATLAB command window and the means of storing and retrieving data from files. Introduce cell arrays. Copyright © Edward B. Magrab 2009

3 Topics Strings and Annotated Output Creating Strings
Converting Numerical Values to Strings and Displaying Them Entering Data With Input Scalar, Vector, Matrix, and String Input/Output Data Files Cell Arrays Input Microsoft Excel Files Copyright © Edward B. Magrab 2009

4 Strings (Literals) - Collections of any combination of letters, numbers, and special characters Created, stored, and manipulated in arrays and defined similar to that for vectors and matrices Differ from an array of numerical values in that – Each character occupies one element in an array Defined by enclosing all its characters between a pair of single quotes (' … ') Typically used for – Displaying information to the command window Annotating data displayed to the command window Annotating graphs Copyright © Edward B. Magrab 2009

5 Examples – Let s be the string testing123. The MATLAB syntax is
s = 'testing123' or s = ['testing123'] To retrieve specific characters in the string s, we can use expressions like s(7)  g s(3:6)  stin Strings can also be concatenated (added to form a longer string) as sc = ['testing123', 'testing123'] which produces the (120) string sc = testing123testing123 Copyright © Edward B. Magrab 2009

6 sc = ['testing123'; 'testing123'] creates the (210) matrix scs =
But, sc = ['testing123'; 'testing123'] creates the (210) matrix scs = testing123 Thus, scs(1,:)  testing123 scs(2,:)  testing123 Copyright © Edward B. Magrab 2009

7 One can find the starting locations of strings within strings using
findstr(string1, string2) which searches the longer of the two strings for the occurrences of the shorter of the two strings. Let us find the occurrences of ‘123’ in the concatenated string shown in the script below sc = ['testing123', 'testing123'] Loc=findstr(sc, '123') Upon execution, we obtain sc = testing123testing123 Loc = Copyright © Edward B. Magrab 2009

8 Thus, if we have the expression lab = ['first ';'last ';'middle'] then
If we place a string in each row of a matrix, we have a convenient way in which to access string expressions. The requirement is that each row must contain the same number of characters. This requirement can be met by employing blanks to pad the rest of the string when the individual string expressions are of unequal length. Thus, if we have the expression lab = ['first ';'last ';'middle'] then lab(1,: )  firstb lab(2,: )  lastbb lab(3,: )  middle and b indicates a blank space. Copyright © Edward B. Magrab 2009

9 The padding is performed by char
Thus, the above expression can be replaced by the easier-to-use expression lab = char('first','last','middle') ord = size(lab) which, when executed, displays lab = first last middle ord = The trailing blanks can be removed with deblank Copyright © Edward B. Magrab 2009

10 The leading and trailing blanks can be removed with strtrim(s)
where s is a string. Two strings can be compared by using L = strcmp(A, B) where A and B are strings and L = 1 (true) if A = B L = 0 (false) if A  B This function is intended to compare character data. Copyright © Edward B. Magrab 2009

11 Example – In the two strings defined in the script, A has two additional leading blanks and two additional trailing blanks than string B. A = ' Yes and No '; B = ' Yes and No '; C1 = strcmp(A, B) C2 = strcmp(strtrim(A), strtrim(B)) which, upon execution, gives C1 = C2 = 1 Copyright © Edward B. Magrab 2009

12 Converting Numerical Values to Strings and Displaying Them
To convert a numerical value to a string, we use z = num2str(num) or z = num2str(num,N) where z is a string num is either a number, an array of numbers, or an expression resulting in a number or an array of numbers N is number of digits to be displayed (if omitted, N = 5) Copyright © Edward B. Magrab 2009

13 Example - Let a = 1000 = Then the various values of N will display the digits shown below. num2str(a,1)  3e+003 num2str(a,3)  3.14e+003 num2str(a,4)  3142 num2str(a,5)  num2str(a,8)  Notice that the decimal point (.) does not count as a digit. Copyright © Edward B. Magrab 2009

14 Example - Let a =  /1000 = Then the various values of N will display the digits shown below num2str(a,1)  0.003 num2str(a,3)  num2str(a,4)  num2str(a,5)  num2str(a,8)  Copyright © Edward B. Magrab 2009

15 To convert an integer to a string, we use z = int2str(num)
where num is an integer. If num is not an integer, then it is rounded to one. Copyright © Edward B. Magrab 2009

16 disp(['Product weight = ' num2str(num) ' kg'])
A typical construct is to use num2str to concatenate the converted numerical value with some identifying text. Thus, if num is, say, the weight in kilograms, and it is to be identified as such, then to display it to the command window we use disp as follows: num = ; disp(['Product weight = ' num2str(num) ' kg']) Upon execution, we obtain Product weight = kg Note: At least one blank space on each side of num2str is required. Copyright © Edward B. Magrab 2009

17 Let num be a vector of the lengths of the object.
Then, the script that displays a vector of values is num = [12.567, 3.458, 9.111]; disp(['Object length = ' num2str(num) ' m']) Upon execution, we obtain Object length = m Copyright © Edward B. Magrab 2009

18 n = length(num); % Let MATLAB do the counting
To create annotation that accompanies each value of num, we use repmat as follows. num = [12.567, 3.458, 9.111]; n = length(num); % Let MATLAB do the counting disp([repmat('Object length = ', n, 1) num2str(num') repmat(' m', n, 1)]) which, upon execution, displays Object length = m Object length = m Object length = m Copyright © Edward B. Magrab 2009

19 fprintf(1,'%…', variables) where
An alternative way to display formatted data to the MATLAB command window is with fprintf(1,'%…', variables) where ‘1’ indicates that the output goes to the command window '%….' is the format specification pertaining to variables % precedes each specific format specification, which is of the form x.yq q specifies the format x specifies the minimum number of digits to be displayed y is the number of digits to the right of the decimal point Copyright © Edward B. Magrab 2009

20 Example – We select q = f, which is a fixed point format, and use it to display the vector num = [12, -14, , ]; several different ways. To display this vector on one line, we have fprintf(1,'%5.2f ', num) which results in Copyright © Edward B. Magrab 2009

21 which, when executed, displays 12.00 -14.00 3098.46 0.11
To display these values as a column of four numbers, we use the delimiter \n as follows: num = [ ]; fprintf(1,'%5.2f\n', num) which, when executed, displays 12.00 -14.00 0.11 Copyright © Edward B. Magrab 2009

22 The execution of this script results in weight = 12 kg
To display each number with the appropriate number of digits, annotate each value, and print it in column form, we use num = [12, -14, , ]; fprintf(1,'weight = %2.0f kg\npressure = %2.0f kPa\ntime = %5.3f s\nlength = %5.5f m', num) The execution of this script results in weight = 12 kg pressure = -14 kPa time = s length = m Copyright © Edward B. Magrab 2009

23 If this number were x = 0.00045, then the script is x = 0.00045;
num2str can also employ the format specifications of fprintf by replacing the second argument N in num2str with a string format specification. Suppose that we wanted to display a very small number as 0 instead of in exponent form. If this number were x = , then the script is x = ; disp(['x = ' num2str(x,'%2.1f')]) displays x = 0.0 Copyright © Edward B. Magrab 2009

24 Entering Data with input
Entering a Scalar – To input a single numerical quantity, we use InputData=input('Enter temperature in degrees C: '); Upon execution, we have displayed in the command window Enter temperature in degrees C: where the number was entered by the user. The semicolon at the end of the expression in the script suppresses the echoing of the value entered. The variable InputData has the value of after Enter is depressed. Copyright © Edward B. Magrab 2009

25 One can also modify user-entered values in the same expression.
Consider the conversion of degrees to radians. Here InputData = input('Enter the starting angle in degrees: ')*pi/180; When executed, we have in the command window Enter the starting angle in degrees: 45 where the value 45 was entered by the user. However, the value of InputData is (= 45/180). Copyright © Edward B. Magrab 2009

26 Entering a String – To input a single string quantity, we append an 's' at the end of the input function InputData = input('Enter file name, including its extension: ','s'); which displays command to the window. Enter file name, including its extension: DataSet3.txt where the string DataSet3.txt was entered by the user. Notice that no single quotation marks are required. The value of InputData is the string DataSet3.txt, which is a vector of length 12. Copyright © Edward B. Magrab 2009

27 The square brackets are required.
Entering a Vector – To input a vector of numerical values, we use InputData = input('Enter four temperatures in degrees C: '); which, upon execution, displays Enter four temperatures in degrees C: [120, 141,169, 201] where the vector of numbers [120, 141, 169, 201] was entered by the user. The square brackets are required. If a column vector was required, then the user's response would be either [120, 141, 169, 201]' or [120; 141; 169; 201] Copyright © Edward B. Magrab 2009

28 Entering a Matrix - To input a matrix of numerical values, we use
InputData = input('Enter three temperatures in degrees C\nfor levels 1 and 2: '); which displays Enter the three temperatures in degrees C for levels 1 and 2: [67, 35, 91; 44, 51, 103] where the array [67, 35, 91; 44, 51, 103] was entered by the user. The variable InputData is a (23) array. Copyright © Edward B. Magrab 2009

29 Input/Output Data Files – load/save
Reads data on a row-by-row basis. Each row separated by an Enter, where Enter is used instead of the semicolon. Each data value separated by either one or more blanks or by a comma. The number of columns of data in each row must be the same. For a row vector, data entered without using Enter. For a column vector, each data value followed by an Enter. Copyright © Edward B. Magrab 2009

30 Example – Assume that data reside in an ASCII text file DataSection33.txt in the form The load function is load('DataSection33.txt') Copyright © Edward B. Magrab 2009

31 load('DataSection33.txt') y = DataSection33.^2
Let us square each element of the matrix in DataSection33.txt. The script is load('DataSection33.txt') y = DataSection33.^2 which, upon execution, results in y = Copyright © Edward B. Magrab 2009

32 FileName1 = input('Enter file name containing data
If one wanted to operate on data in different files, each having a different file name, then we have to employ a different technique. Here, the user will enter the file name when requested to do so and, as before, the script will square the data residing in the file whose name is specified when the script is executed. The script is FileName1 = input('Enter file name containing data (including suffix): ','s'); load(FileName1); m = findstr(FileName1,'.'); data1 = eval(FileName1(1:m-1)); y = data1.^2 Copyright © Edward B. Magrab 2009

33 Since FileName1 is still unknown to the remaining expressions in the script, it must be converted to a numerical quantity. This is done by the eval function, which evaluates the string quantity appearing in its argument. Copyright © Edward B. Magrab 2009

34 save If one wants to save numerical values resulting from the execution of a script or function to a file, then we use save('File name', 'Variable 1', 'Variable 2', …, '-ascii') where 'File name' is a string containing the name of the file to be saved and its directory, if other that the current directory. 'Variable n' are strings containing the names of the n variables that are to be saved in the order that they appear. '-ascii' is a string indicating that the data will be saved in ascii format. Copyright © Edward B. Magrab 2009

35 Example – Save the square of each value in a file named DataSection33.txt as ASCII text. The script is load('DataSection33.txt') y = DataSection33.^2; save('SavedDataSection33.txt', 'y', '-ascii') Upon execution, the script creates a text file whose contents are e e e+002 e e e+002 e e e+003 e e e+003 Copyright © Edward B. Magrab 2009

36 A Note About Path Name – When just the file name is given, MATLAB places the file in the current directory. In order to place the file in a specific directory, the entire path name must be given. For example, load('DataSection33.txt') % File in current directory y = DataSection33.^2; z = sqrt(Datasection33); save('c:\Matlab mfiles\Matlab results\SavedDataSection33.txt', 'y', ‘z', '-ascii') Copyright © Edward B. Magrab 2009

37 Execution of this script creates the file SavedDataSection331
Execution of this script creates the file SavedDataSection331.txt with the contents e e e+002 e e e+002 e e e+003 e e e+003 e e e+000 e e e+000 e e e+000 e e e+000 y z Copyright © Edward B. Magrab 2009

38 Cell Arrays – Cell arrays are a special class of arrays whose elements consist of cells that themselves contain arrays. They provide a hierarchical way of storing dissimilar kinds of data. Any cell in a cell array can be accessed through matrix indexing as is done with standard vectors and matrices. Cell notation differs from standard matrix notation in that open braces ‘{’ and the closed braces ‘}’ are used instead of open ‘[’ and closed ‘]’ brackets. Copyright © Edward B. Magrab 2009

39 Example – Let us create four different arrays of data as shown in the following script A = ones(3,2) B = magic(3) C = char('Pressure', 'Temperature', 'Displacement') D = [6+7j, 15] Upon executing this script, we obtain Copyright © Edward B. Magrab 2009

40 A = 1 1 B = 8 1 6 3 5 7 4 9 2 C = Pressure Temperature Displacement
B = C = Pressure Temperature Displacement D = i Copyright © Edward B. Magrab 2009

41 C = char('Pressure', 'Temperature', 'Displacement'); D = [6+7j, 15];
We now add to this script the cell assignment statement to create a (22) cell array, which is analogous to that used for standard arrays, except that we use braces as delimiters. Thus, A = ones(3,2); B = magic(3); C = char('Pressure', 'Temperature', 'Displacement'); D = [6+7j, 15]; Cel = {A, B; C, D} After executing this script, we obtain Cel = [3x2 double] [3x3 double] [3x12 char ] [1x2 double] Copyright © Edward B. Magrab 2009

42 To display the contents of Cel to the command window, we use
Notice that we do not get what is specifically in each cell, only what the size of the data arrays in each cell are and their type. To display the contents of Cel to the command window, we use celldisp(x) Then, the program becomes A = ones(3,2); B = magic(3); C = char('Pressure', 'Temperature', 'Displacement'); D = [6+7j, 15]; Cel = {A, B; C, D}; celldisp(Cel) Copyright © Edward B. Magrab 2009

43 which, upon execution, gives Cel{1,1} = 1 1
Cel{2,1} = Pressure Temperature Displacement Cel{1,2} = Cel{2,2} = i Copyright © Edward B. Magrab 2009

44 C = char('Pressure', 'Temperature', 'Displacement'); D = [6+7j,15];
To access each cell individually, we use the index notation used for standard array variables, except that we use braces instead of parentheses. Thus, A = ones(3,2); B = magic(3); C = char('Pressure', 'Temperature', 'Displacement'); D = [6+7j,15]; Cel = {A, B; C, D}; Cell_1_2 = Cel{1,2} which, upon execution, gives Cell_1_2 = Copyright © Edward B. Magrab 2009

45 Words = {'application', 'apple', 'friend', 'apply', 'fiend'};
With cell arrays, the use of sort can be extended to sort words in dictionary order. Consider the following script, where we will create a cell array of five words and then sort them. Words = {'application', 'apple', 'friend', 'apply', 'fiend'}; WordSort = sort(Words)' The execution of this script gives 'apple' 'application' 'apply' 'fiend' 'friend' Copyright © Edward B. Magrab 2009

46 Input Microsoft Excel Files
Data files created in Microsoft Excel can be read into MATLAB with the function [X, Y] = xlsread('Filename') where X will be an array containing the columns and rows of data and Y will be a cell array containing any text headers that accompany the data. The file name must contain the suffix ‘.xls’. Copyright © Edward B. Magrab 2009

47 Example – Consider the data generated in Excel shown below and saved in ForceDispData.xls. The script to read this file is [X, Y] = xlsread('ForceDispData.xls') X = Y = [1x20 char] [] 'Force' 'Displacement' '(kPa)' '(mm)' Note: Y is a (3×2) cell array: Y{1,1}=Transducer Linearity Copyright © Edward B. Magrab 2009


Download ppt "AN ENGINEER’S GUIDE TO MATLAB"

Similar presentations


Ads by Google