Chapter 5 File and Directory Management 5 1 The MATLAB Workspace User commands and resulting output in the MATLAB Command window can be logged 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 1 diary klee sept6 x 1 y 2 z x y A 5 B 10 C A B diary off Save MATLAB session in file klee sept6 z A B C 3 5 10 50 The entire Command window or a highlighted portion of it can be printed using either Print or Print Selection from the Command window File menu All variables and their values present in the MATLAB Workspace at the end of a MATLAB session can be saved using Save Workspace as from the File menu of the Command window To load the same variables and their stored values in the MATLAB Workspace the Load Workspace menu item from the File menu is used 1 5 2 Saving Loading and Deleting Files Specific variables from the MATLAB Workspace can be saved in a choice of formats using the save command Example 5 2 1 clear 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 clear rad 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 clear l 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 from saved mat files can be loaded into the MATLAB Workspace using the load command Example 5 2 2 clear load Loads all variables from file matlab mat into MATLAB Workspace Loading from matlab mat clear load 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 2 load 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 on MATLAB s search path the exist command is issued and returns a value of 2 otherwise a zero is returned Example 5 2 3 clear x 0 25 sum 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 2500 sum 1 3333 ans 0 ans 2 The variables stored in a MATLAB data file mat extension can be obtained from the Command window using the whos command along with the file name Example 5 2 4 whos file geoseries mat Name Size sum x 1x1 1x1 Bytes 8 8 Class double array double array Grand total is 2 elements using 16 bytes Finally MATLAB data files which are no longer needed can be deleted from within MATLAB with the delete command Example 5 2 5 delete geoseries mat 3 5 3 Special Purpose File I O The MATLAB functions dlmread and dlwrite are designed to be used with asccii files of numerical values separated by a user specified delimiter Example 5 3 1 A 10 rand 5 dlmwrite random num dat A Create ascii file random num dat and store values from array A in it separated by commas B dlmread random num dat Open ascii file random num dat read comma delimited data and store in array B A B 1 5683 4 1635 0 9403 4 4995 8 6915 3 9162 2 5278 3 5438 7 4298 6 5083 9 3979 8 3280 4 6998 6 2987 0 5819 5 4219 4 5573 8 6309 8 5520 4 7226 7 8692 6 5598 0 0004 1 3124 4 9487 1 5683 4 1635 0 9404 4 4995 8 6915 3 9162 2 5278 3 5438 7 4298 6 5083 9 3979 8 3280 4 6998 6 2987 0 5819 5 4219 4 5573 8 6309 8 5520 4 7226 7 8692 6 5598 0 0004 1 3124 4 9487 Popular spreadsheet programs Excel Lotus etc store data in a rectangular grid of rows and columns using Lotus WK1 spreadsheet format The command wk1write can store MATLAB numeric arrays in WK1 spreadsheet format A description 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 2 M rand 3 6 Create 3 6 array of random numbers wk1write 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 4 M 0 0383 0 2274 0 3279 0 8995 0 3137 0 2517 The created file processing 0 4330 0 8424 0 1845 0 5082 0 4522 0 3256 0 3801 0 8865 0 7613 0 8838 0 4574 0 7992 klee data xls can be opened in EXCEL for further The command wk1read opens files created by a spreadshet application and reads the data into a MATLAB variable The help command to learn more about reading spreadsheet files saved in WK1 format is help 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 read and R2 C2 is the lower right corner RNG can also be specified using spreadsheet notation as in RNG A1 B7 or a named range like Sales See also WK1WRITE CSVREAD CSVWRITE Example 5 3 3 P wk1read klee data xls 3 7 Read spreadsheet klee data xls starting in Row 4 Column 8 H and …
View Full Document