DOC PREVIEW
UCF EGN 3420 - File and Directory Management

This preview shows page 1-2-19-20 out of 20 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 20 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Chapter 5 File and Directory Management5.1 The MATLAB WorkspaceUser commands and resulting output in the MATLAB Command window can belogged and saved as an ascii text file in the current folder. The 'diary fname'command keeps a log of the MATLAB session and stores it in the file 'fname' or simply'diary' if the file name is ommitted.Example 5.1.1diary klee_sept6 % Save MATLAB session in file klee_sept6x=1;y=2;z=x+yA=5B=10C=A*Bdiary off z = 3A = 5B = 10C = 50 The entire Command window or a highlighted portion of it can be printed usingeither Print or Print Selection from the Command window File menu. All variables and their values present in the MATLAB Workspace at the end of aMATLAB session can be saved using Save Workspace as ... from the File menu of theCommand window. To load the same variables and their stored values in the MATLABWorkspace, the Load Workspace ... menu item from the File menu is used.15.2 Saving, Loading, and Deleting FilesSpecific variables from the MATLAB Workspace can be saved in a choice offormats using the 'save' command.Example 5.2.1clear price =2.5; volume=2000; revenue=price*volume; save % Saves Workspace variables in binary format in file 'matlab.mat' Saving to: matlab.mat clear rate =5.0; time=1000; dist=rate*time; save trip % Saves Workspace variables in binary format in 'trip.mat' clearrad=3;Area=pi*rad^2;circum=2*pi*rad; save circle Area circum % Saves Workspace variables 'A' and 'circum' % in binary format in file 'circle.mat' clearl=8;w=4;h=3;A=l*w;V=A*h;save box l w h -ascii % Saves Workspace variables 'l', 'w', and 'h' % in 8-digit ascii format in file 'box' Note that the ascii files are not assigned a .mat extension. Selected variables fromsaved .mat files can be loaded into the MATLAB Workspace using the 'load' command.Example 5.2.2clearload % Loads all variables from file 'matlab.mat into MATLAB Workspace Loading from: matlab.mat clearload circle Area circum % Loads variables 'Area' and 'circum' from % file 'circle.mat' into MATLAB Workspace The following command is invalid because an ascii file cannot be loaded into the MATLAB Workspace. It can only be edited in a text editor.2load box l w % Loads variables 'l' and 'w' from ascii file 'box' To see if a particular data file has been saved in the current directory (or onMATLAB's search path) the 'exist' command is issued and returns a value of 2, otherwisea zero is returned.Example 5.2.3clearx=0.25sum=1/(1-x) % Sum of geometric series 1 + x + x^2 + x^3 + ...save geoseries % Save Workspace variables in file 'geoseries.mat'exist('geomseries.mat','file')% Check for existence of 'geomseries.mat'exist('geoseries.mat','file') % Check for existence of 'geoseries.mat' x = 0.2500sum = 1.3333ans = 0ans = 2 The variables stored in a MATLAB data file (.mat extension) can be obtainedfrom the Command window using the 'whos' command along with the file name.Example 5.2.4whos -file geoseries.mat Name Size Bytes Class sum 1x1 8 double array x 1x1 8 double arrayGrand total is 2 elements using 16 bytes Finally, MATLAB data files which are no longer needed can be deleted fromwithin MATLAB with the 'delete' command.Example 5.2.5delete('geoseries.mat') 35.3 Special-Purpose File I/OThe MATLAB functions 'dlmread' and 'dlwrite' are designed to be used withasccii files of numerical values separated by a user specified delimiter.Example 5.3.1A=10*rand(5)dlmwrite('random_num.dat', A, ',') % Create ascii file 'random_num.dat' % and store values from array A in it,separated by commasB=dlmread('random_num.dat', ',') % Open ascii file 'random_num.dat', % read comma delimited data and store in array B A = 1.5683 3.9162 9.3979 5.4219 7.8692 4.1635 2.5278 8.3280 4.5573 6.5598 0.9403 3.5438 4.6998 8.6309 0.0004 4.4995 7.4298 6.2987 8.5520 1.3124 8.6915 6.5083 0.5819 4.7226 4.9487B = 1.5683 3.9162 9.3979 5.4219 7.8692 4.1635 2.5278 8.3280 4.5573 6.5598 0.9404 3.5438 4.6998 8.6309 0.0004 4.4995 7.4298 6.2987 8.5520 1.3124 8.6915 6.5083 0.5819 4.7226 4.9487 Popular spreadsheet programs (Excel, Lotus,etc.) store data in a rectangular gridof rows and columns using 'Lotus WK1 spreadsheet format'. The command'wk1write' can store MATLAB numeric arrays in WK1 spreadsheet format. Adescription of 'wk1write' is obtained using the 'help' command.help wk1write WK1WRITE Write spreadsheet (WK1) file. WK1WRITE('FILENAME',M) writes matrix M into a Lotus WK1 spreadsheet file with the name. '.wk1' is appended to the filename if no extension is given. WK1WRITE('FILENAME',M,R,C) writes matrix M into a Lotus WK1 spreadsheet file, starting at offset row R, and column C in the file. R and C are zero-based so that R=C=0 is the first cell in the spreadsheet. See also WK1READ, DLMREAD, DLMWRITE. Example 5.3.2M=rand(3,6) % Create 36 array of random numberswk1write('klee_data.xls', M, 2,5)% Store MATLAB array M in a WK1 format % file 'klee_data.xls' starting at Row 3, Column 6(F) 4M = 0.0383 0.8995 0.4330 0.5082 0.3801 0.8838 0.2274 0.3137 0.8424 0.4522 0.8865 0.4574 0.3279 0.2517 0.1845 0.3256 0.7613 0.7992 The created file 'klee_data.xls' can be opened in EXCEL for furtherprocessing. The command 'wk1read' opens files created by a spreadshet application andreads the data into a MATLAB variable. The 'help' command to learn more aboutreading spreadsheet files saved in WK1 format ishelp wk1read WK1READ Read spreadsheet (WK1) file. A = WK1READ('FILENAME') reads all the data from a Lotus WK1 spreadsheet file named FILENAME into matrix A. A = WK1READ('FILENAME',R,C) reads data from a Lotus WK1 spreadsheet file starting at row R and column C, into the matrix A. R and C are zero-based so that R=C=0 is the first cell of the spreadsheet. A = WK1READ('FILENAME',R,C,RNG) specifies a cell range or named range for selecting data from the spreadsheet. A cell range is specified by RNG = [R1 C1 R2 C2] where (R1,C1) is the upper-left corner of the data to be


View Full Document

UCF EGN 3420 - File and Directory Management

Download File and Directory Management
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view File and Directory Management and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view File and Directory Management 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?