Unit#8 MATLAB 1 - Engineering Applications | |||||||||||||
|
|||||||||||||
|
|||||||||||||
Reading input from a file: Suppose that we have created a long file of data, which contains 23 lines of numbers, 3 numbers per line. The following picture shows the contents of this file:
Note that the text lines in the header of the table are proceeded by the % sign. In MATLAB the % sign plays the same role as the comment sign “ // “ of C++. Anything after % is ignored by MATLAB. Also empty lines are skipped too. The file deflections.txt is located in a folder on the desktop named matlabDataFiles. In order to have these numbers entered to MATLAB as elements of a 23X3 array, we must first make sure that our MATLAB command window is looking at the right place, in other words we have to set the right directory. We can do this by selecting Set Path from the File menu. The following window will pop up:
From the Current Directory window we must Browse and locate the proper folder on our desktop and then return to the MATLAB command window. See the change in the Path Browser window. The Path Browser window may remain open if we so please, or it can be closed. WE SHOULD NOT CHANGE THE PATH.
At this point we can import the data from the file deflections.txt by simply typing the command: load deflections.txt We can see that the data will be automatically loaded into an array of the proper size which has the same name as the file. We can view the contents of the array by entering its name as it is illustrated in the following picture:
Now we may want to extract parts of this array and use them to define other variables. For example we may want to take the first column and form the 23X1 column array left and take the third column and form another 32X1 array named right. We can do this by the commands: left = deflections ( : , 1 ) and right = deflections( : , 3 ) This is illustrated in the picture below:
Outputting data to a file: Now suppose that what we need to select the center column of the array deflections, and output its values to another file named center.txt in the same folder matlabDataFiles. We need to identify a stream and send the information through that stream to the file. We saw how to do this in C++. The corresponding statement in MATLAB is similar as it is illustrated below:
Typing help fopen in the MATLAB command window, produces the following information for the fopen command :
We can now use the stream mytable to send the information to the table. For this, we write: fprintf ( mytable, ' \n\n The central array is: \n '); fprintf ( mytable, ' \n %f ' , deflections( : , 2 ) ) ;
and at the end we must close the stream by the statement: fclose( mytable ); This sequence is illustrated in the following picture of the MATLAB window . The corresponding report file created is also shown:
resulting report file:
It is now important to understand that the more general form of the fprintf command is: fprintf ( StreamName, ‘ Formatting String ‘ , variables list ) ; For additional information use help fprintf at the MATLAB command window. Prompting input : Sometimes it is important to prompt the user of a script 1 to enter data for a given variable of the program ( say height ) , interactively. This is accomplished by the command input as follows: height = input ( ‘ \n Please enter the height of the room in feet >>> ‘) ; The following picture shows the effect of this:
1 Of course, this prompt does not make much sense when we use the command window in fully interactive mode as we have seen so far. However when we discuss the batch approach ( see next subtopic) of running MATLAB programs ( m – files ), we will see that the prompted input is a very important concept. |
|||||||||||||
Jacob Y. Kazakia © 2001 All rights reserved |