matlab basics data files
play

MATLAB Basics: Data Files save filename var1 var2 binary save - PDF document

MATLAB Basics: Data Files save filename var1 var2 binary save homework.mat x y Input/Output Functions ascii save x.dat x ascii load filename Selim Aksoy binary load filename.mat Bilkent University ascii


  1. MATLAB Basics: Data Files � save filename var1 var2 … → binary � save homework.mat x y Input/Output Functions → ascii � save x.dat x –ascii � load filename Selim Aksoy → binary � load filename.mat Bilkent University → ascii � load x.dat –ascii Department of Computer Engineering saksoy@cs.bilkent.edu.tr Spring 2004 CS 111 2 The textread Function The textread Function � It is designed to read ASCII files that � [a,b,c,…] = textread(filename,format,n) are formatted into columns of data � filename: a string that is the name of the file to be read � Each column can be of a different type � format: a string containing the format � It is useful for importing tables of data primitives (just like in fprintf) printed out by other applications � n: number of lines to read (if not specified, the file is read until the end) Spring 2004 CS 111 3 Spring 2004 CS 111 4 The textread Function The textread Function � Example: Assume that you have a file � [fname,lname,rank,phone] = called phones.txt textread( 'phones.txt', '%s %s % s % d' ) VarolAkman Prof 1538 � phone = � fname = Selim Aksoy AsstProf 3405 1537 'Varol ‘ Erol Arkun Prof 2249 3405 'Selim‘ Cevdet Aykanat Prof 1625 2249 'Erol‘ Mehmet Baray Prof 1208 1625 'Cevdet ‘ Cengiz Çelik Instructor 2613 1208 Ilyas Çiçekli AsstProf 1589 'Mehmet‘ David Davenport AsstProf 1248 2613 'Cengiz‘ ... ... ... cell array double array Spring 2004 CS 111 5 Spring 2004 CS 111 6

  2. The textread Function The textread Function � The textread function skips the columns � Example: Searching for telephone that have an asterisk (* ) in the format numbers descriptor name = ‘Selim’; for ii = 1:length(fname), � [fname, phone] = if ( strcmp( fname(ii), name ) ), textread( 'phones.txt', '% s % * s % * s % d' ) disp( phone(ii) ); � The load command (with ASCII option) end assumes all of the data is of a single end be careful about the type but textread is more flexible usage of cell arrays Spring 2004 CS 111 7 Spring 2004 CS 111 8 File Processing Opening Files � fid = fopen( filename, permission ) � File types: opens the file filename in the mode specified � Binary files by permission � Data is stored in program readable format � fid is the file id (a positive integer) that is assigned � Processing is fast to the file by MATLAB � Text (ASCII) files � fid is used for all reading, writing and control � Data is stored in human readable format operations on that file � Processing is slower � file id 1 is the standard output device and file id 2 is the standard error device � fid will contain -1 if the file could not be opened Spring 2004 CS 111 9 Spring 2004 CS 111 10 Opening Files Opening Files � Permission can be: � Examples: � ‘r’: open file for reading (default) � fid = fopen( ‘example.dat’, ‘r’ ) � ‘w’: open file, or create a new file, for writing; opens a binary file for input discard existing contents, if any � fid = fopen( ‘example.dat’, ‘wt’ ) � ‘a’: open file, or create a new file, for writing; append data to the end of the file opens a text file for output (if example.dat � ‘r+ ’: open file for reading and writing already exists, it will be deleted) � ‘w+ ’: open file, or create a new file, for reading � fid = fopen( ‘example.dat’, ‘at’ ) and writing; discard existing contents, if any opens a text file for output (if example.dat � ‘a+ ’: open file, or create a new file, for reading already exists, new data will be appended and writing; append data to the end of the file � Add ‘t’ to the permission string for a text file to the end) Spring 2004 CS 111 11 Spring 2004 CS 111 12

  3. Closing Files Writing Binary Data � count = fwrite( fid, array, precision ) � status = fclose( fid ) writes data in array in binary format closes the file with file id fid � fid: file id of the file opened using fopen � If the closing operation is successful, � array: array of values to write status will be 0 � count: number of values written to the file � If the closing operation is unsuccessful, � MATLAB writes data in column order (if status will be -1 array is [1 2;3 4;5 6], data is written as � status = fclose( ‘all’ ) 1, 3, 5, 2, 4, 6) closes all open files (except for standard � You can use array’ to write in row order output and standard error) Spring 2004 CS 111 13 Spring 2004 CS 111 14 Writing Binary Data Reading Binary Data � [array,count] = fread(fid,size,precision) � Precision (platform independent) can be: reads binary data in a user-specified � ‘char’: 8-bit characters format � ‘uchar’: 8-bit unsigned characters � size: number of values to read � ‘int16’: 16-bit integer � n: read exactly n values (array will be a column � ‘int32’: 32-bit integer vector with length n) � Inf: read until the end of the file (column vector) � ‘uint16’: 16-bit unsigned integer � [n m]: read exactly n x m values (array will be a � ‘uint32’: 32-bit unsigned integer n-by-m matrix) � ‘float32’: 32-bit floating point � array: array that contains the data � ‘float64’: 64-bit floating point � count: number of values read from the file Spring 2004 CS 111 15 Spring 2004 CS 111 16 Binary I/O Examples Binary I/O Examples %Randomly generate 100 passwords with 8 characters % Script file: binary_io.m % Purpose: To illustrate the use of binary i/o functions. passwords = []; % Prompt for file name for ii = 1:100, %Generate each password filename = input('Enter file name: ','s'); s = []; % Generate the data array for jj = 1:8, %Generate each character of each password out_array = randn(1,10000); t = char( round ( ('z' - 'a') *rand + 'a' ) ); % Open the output file for writing. s = [ s t ]; [fid,msg] = fopen(filename,'w'); end % Was the open successful? passwords = strvcat( passwords, s ); if fid > 0 % Write the output data. end count = fwrite(fid,out_array,'float64'); % Tell user disp([int2str(count) ' values written...']); %Write passwords to a binary file passwd.dat % Close the file status = fclose(fid); %Open the file else [ fid, msg ] = fopen( 'passwd.dat', 'wb' ); % Output file open failed. Display message. disp(msg); if ( fid < 1 ), end error( msg ); % Now try to recover the data. Open the file for reading. end [fid,msg] = fopen(filename,'r'); %Write the passwords % Was the open successful? if fid > 0 count = fwrite( fid, passwords, 'char' ); % Read the input data. fprintf( '%d characters were written\n', count ); [in_array, count] = fread(fid,[100 100],'float64'); % Tell user %Close the file disp([int2str(count) ' values read...']); status = fclose( fid ); % Close the file status = fclose(fid); if ( status ~ = 0 ), else % Input file open failed. Display message. error( 'Could not close the file' ); disp(msg); end end Spring 2004 CS 111 17 Spring 2004 CS 111 18

  4. Binary I/O Examples Writing Formatted Text Data %Read the passwords from the binary file passwd.dat � count = fprintf(fid,format,val1,val2,…) %Open the file [ fid, msg ] = fopen( 'passwd.dat', 'rb' ); writes formatted text data in a user- if ( fid < 1 ), error( msg ); specified format end %Read the passwords � fid: file id (if fid is missing, data is written [ passwords2, count ] = fread( fid, [100 8], 'char' ); fprintf( '%d characters were read\n', count ); to the standard output device (command %Close the file status = fclose( fid ); window) if ( status ~ = 0 ), error( 'Could not close the file' ); � format: same as what we have been using end (combination of format specifiers that start with % ) � count: number of characters written Spring 2004 CS 111 19 Spring 2004 CS 111 20 Writing Formatted Text Data Reading Formatted Text Data � Make sure there is a one-to-one � [array,count] = fscanf(fid,format,size) correspondence between format specifiers reads formatted text data in a user- and types of data in variables specified format � Format strings are scanned from left to right � fid: file id � Program goes back to the beginning of the � format: same as format in fprintf format string if there are still values to write � size: same as size in fread (format string is recycled) � array: array that receives the data � If you want to print the actual % character, � count: number of elements read you can use % % in the format string Spring 2004 CS 111 21 Spring 2004 CS 111 22 Reading Formatted Text Data Reading Formatted Text Data � line = fgetl( fid ) � line = fgets( fid ) reads the next line excluding the end- reads the next line including the end-of- of-line characters from a file as a line characters from a file as a character character string string � line: character array that receives the data � line: character array that receives the data � line is set to -1 if fgetlencounters the end � line is set to -1 if fgets encounters the end of a file of a file Spring 2004 CS 111 23 Spring 2004 CS 111 24

Recommend


More recommend