type write_file.m % % write_file.m % % -MCW Fall 2016 % myfile = fopen('exampledata.z', 'w'); if (myfile == -1) error('File open did not work.'); return end fwrite(myfile, 'random data!', 'uchar'); if (fclose(myfile) == -1) error('File did not close'); end ls -l exampledata* {Error using ls (line 35) ls: exampledata*: No such file or directory } write_file ls -l exampledata* -rw-r--r-- 1 mweeks staff 12 Nov 10 15:40 exampledata.z type exampledata.z random data! pwd ans = /Users/mweeks/Documents/MATLAB cd /Users/mweeks/Desktop help fopen fopen Open file. FID = fopen(FILENAME) opens the file FILENAME for read access. FILENAME is a string containing the name of the file to be opened. (On PC systems, fopen opens files for binary read access.) FILENAME can be a MATLABPATH relative partial pathname. If the file is not found in the current working directory, fopen searches for it on the MATLAB search path. On UNIX systems, FILENAME may also start with a "~/" or a "~username/", which fopen expands to the current user's home directory or the specified user's home directory, respectively. FID is a scalar MATLAB integer valued double, called a file identifier. You use FID as the first argument to other file input/output routines, such as FREAD and FCLOSE. If fopen cannot open the file, it returns -1. FID = fopen(FILENAME,PERMISSION) opens the file FILENAME in the mode specified by PERMISSION: 'r' open file for reading 'w' open file for writing; discard existing contents 'a' open or create file for writing; append data to end of file 'r+' open (do not create) file for reading and writing 'w+' open or create file for reading and writing; discard existing contents 'a+' open or create file for reading and writing; append data to end of file 'W' open file for writing without automatic flushing 'A' open file for appending without automatic flushing FILENAME can be a MATLABPATH relative partial pathname only if the file is opened for reading. You can open files in binary mode (the default) or in text mode. In binary mode, no characters get singled out for special treatment. In text mode on the PC, the carriage return character preceding a newline character is deleted on input and added before the newline character on output. To open a file in text mode, append 't' to the permission string, for example 'rt' and 'w+t'. (On Unix, text and binary mode are the same, so this has no effect. On PC systems this is critical.) If the file is opened in update mode ('+'), you must use an FSEEK or FREWIND between an input command like FREAD, FSCANF, FGETS, or FGETL and an output command like FWRITE or FPRINTF. You must also use an FSEEK or FREWIND between an output command and an input command. Two file identifiers are automatically available and need not be opened. They are FID=1 (standard output) and FID=2 (standard error). [FID, MESSAGE] = fopen(FILENAME,...) returns a system dependent error message if the open is not successful. [FID, MESSAGE] = fopen(FILENAME,PERMISSION,MACHINEFORMAT) opens the specified file with the specified PERMISSION and treats data read using FREAD or data written using FWRITE as having a format given by MACHINEFORMAT. MACHINEFORMAT is one of the following strings: 'native' or 'n' - local machine format - the default 'ieee-le' or 'l' - IEEE floating point with little-endian byte ordering 'ieee-be' or 'b' - IEEE floating point with big-endian byte ordering 'ieee-le.l64' or 'a' - IEEE floating point with little-endian byte ordering and 64 bit long data type 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte ordering and 64 bit long data type. [FID, MESSAGE] = fopen(FILENAME,PERMISSION,MACHINEFORMAT,ENCODING) opens the specified file using the specified PERMISSION and MACHINEFORMAT. ENCODING is a string that specifies the character encoding scheme associated with the file. It must be the empty string ('') or a name or alias for an encoding scheme. Some examples are 'UTF-8', 'latin1', 'US-ASCII', and 'Shift_JIS'. For common names and aliases, see the Web site http://www.iana.org/assignments/character-sets. If ENCODING is unspecified or is the empty string (''), MATLAB's default encoding scheme is used. [FILENAME,PERMISSION,MACHINEFORMAT,ENCODING] = fopen(FID) returns the filename, permission, machine format, and character encoding values used by MATLAB when it opened the file associated with identifier FID. MATLAB does not determine these output values by reading information from the opened file. For any of these parameters that were not specified when the file was opened, MATLAB returns its default value. The ENCODING string is a standard character encoding scheme name that may not be the same as the ENCODING argument used in the call to fopen that opened the file. An invalid FID returns empty strings for all output arguments. FIDS = fopen('all') returns a row vector containing the file identifiers for all the files currently opened by the user (but not 1 or 2). The 'W' and 'A' permissions do not automatically perform a flush of the current output buffer after output operations. See also fclose, ferror, fgetl, fgets, fprintf, fread, fscanf, fseek, ftell, fwrite. Overloaded methods: serial/fopen i2c/fopen icinterface/fopen Reference page in Help browser doc fopen read_file {Error using read_file (line 10) File "" open did not work. } read_file {Error using read_file (line 10) File "exampledata.zzz" open did not work. } read_file type read_file.m % % read_file.m % % -MCW Fall 2016 % fname = 'exampledata.z'; myfile = fopen(fname, 'r'); if (myfile == -1) error(sprintf('File "%s" open did not work.', fname)); return end [mybuffer, num_read] = fread(myfile, 512, 'uchar'); disp(sprintf('Read %d characters', num_read)); disp(sprintf('Contents are %s ', mybuffer)); if (fclose(myfile) == -1) error('File did not close'); end read_file Read 12 characters Contents are random data! help fopen fopen Open file. FID = fopen(FILENAME) opens the file FILENAME for read access. FILENAME is a string containing the name of the file to be opened. (On PC systems, fopen opens files for binary read access.) FILENAME can be a MATLABPATH relative partial pathname. If the file is not found in the current working directory, fopen searches for it on the MATLAB search path. On UNIX systems, FILENAME may also start with a "~/" or a "~username/", which fopen expands to the current user's home directory or the specified user's home directory, respectively. FID is a scalar MATLAB integer valued double, called a file identifier. You use FID as the first argument to other file input/output routines, such as FREAD and FCLOSE. If fopen cannot open the file, it returns -1. FID = fopen(FILENAME,PERMISSION) opens the file FILENAME in the mode specified by PERMISSION: 'r' open file for reading 'w' open file for writing; discard existing contents 'a' open or create file for writing; append data to end of file 'r+' open (do not create) file for reading and writing 'w+' open or create file for reading and writing; discard existing contents 'a+' open or create file for reading and writing; append data to end of file 'W' open file for writing without automatic flushing 'A' open file for appending without automatic flushing FILENAME can be a MATLABPATH relative partial pathname only if the file is opened for reading. You can open files in binary mode (the default) or in text mode. In binary mode, no characters get singled out for special treatment. In text mode on the PC, the carriage return character preceding a newline character is deleted on input and added before the newline character on output. To open a file in text mode, append 't' to the permission string, for example 'rt' and 'w+t'. (On Unix, text and binary mode are the same, so this has no effect. On PC systems this is critical.) If the file is opened in update mode ('+'), you must use an FSEEK or FREWIND between an input command like FREAD, FSCANF, FGETS, or FGETL and an output command like FWRITE or FPRINTF. You must also use an FSEEK or FREWIND between an output command and an input command. Two file identifiers are automatically available and need not be opened. They are FID=1 (standard output) and FID=2 (standard error). [FID, MESSAGE] = fopen(FILENAME,...) returns a system dependent error message if the open is not successful. [FID, MESSAGE] = fopen(FILENAME,PERMISSION,MACHINEFORMAT) opens the specified file with the specified PERMISSION and treats data read using FREAD or data written using FWRITE as having a format given by MACHINEFORMAT. MACHINEFORMAT is one of the following strings: 'native' or 'n' - local machine format - the default 'ieee-le' or 'l' - IEEE floating point with little-endian byte ordering 'ieee-be' or 'b' - IEEE floating point with big-endian byte ordering 'ieee-le.l64' or 'a' - IEEE floating point with little-endian byte ordering and 64 bit long data type 'ieee-be.l64' or 's' - IEEE floating point with big-endian byte ordering and 64 bit long data type. [FID, MESSAGE] = fopen(FILENAME,PERMISSION,MACHINEFORMAT,ENCODING) opens the specified file using the specified PERMISSION and MACHINEFORMAT. ENCODING is a string that specifies the character encoding scheme associated with the file. It must be the empty string ('') or a name or alias for an encoding scheme. Some examples are 'UTF-8', 'latin1', 'US-ASCII', and 'Shift_JIS'. For common names and aliases, see the Web site http://www.iana.org/assignments/character-sets. If ENCODING is unspecified or is the empty string (''), MATLAB's default encoding scheme is used. [FILENAME,PERMISSION,MACHINEFORMAT,ENCODING] = fopen(FID) returns the filename, permission, machine format, and character encoding values used by MATLAB when it opened the file associated with identifier FID. MATLAB does not determine these output values by reading information from the opened file. For any of these parameters that were not specified when the file was opened, MATLAB returns its default value. The ENCODING string is a standard character encoding scheme name that may not be the same as the ENCODING argument used in the call to fopen that opened the file. An invalid FID returns empty strings for all output arguments. FIDS = fopen('all') returns a row vector containing the file identifiers for all the files currently opened by the user (but not 1 or 2). The 'W' and 'A' permissions do not automatically perform a flush of the current output buffer after output operations. See also fclose, ferror, fgetl, fgets, fprintf, fread, fscanf, fseek, ftell, fwrite. Overloaded methods: serial/fopen i2c/fopen icinterface/fopen Reference page in Help browser doc fopen help ftell ftell Get file position indicator. POSITION = ftell(FID) returns the location of the file position indicator in the specified file. Position is indicated in bytes from the beginning of the file. If -1 is returned, it indicates that the query was unsuccessful. Use FERROR to determine the nature of the error. FID is an integer file identifier obtained from FOPEN. See also ferror, fopen, fprintf, fread, frewind, fscanf, fseek, fwrite. Reference page in Help browser doc ftell read_file Read 10 characters Contents are random dat read_file Read 10 characters Contents are random dat So far, we have read 10 characters help fread fread Read binary data from file. A = fread(FID) reads binary data from the specified file and writes it into matrix A. FID is an integer file identifier obtained from FOPEN. MATLAB reads the entire file and positions the file pointer at the end of the file (see FEOF for details). A = fread(FID,SIZE) reads the number of elements specified by SIZE. Valid entries for SIZE are: N read N elements into a column vector. inf read to the end of the file. [M,N] read elements to fill an M-by-N matrix, in column order. N can be inf, but M can't. A = fread(FID,SIZE,PRECISION) reads the file according to the data format specified by the string PRECISION. The PRECISION input commonly contains a datatype specifier like 'int' or 'float', followed by an integer giving the size in bits. The SIZE argument is optional when using this syntax. Any of the following strings, either the MATLAB version, or their C or Fortran equivalent, may be used. If not specified, the default precision is 'uint8'. MATLAB C or Fortran Description 'uchar' 'unsigned char' unsigned integer, 8 bits. 'schar' 'signed char' signed integer, 8 bits. 'int8' 'integer*1' integer, 8 bits. 'int16' 'integer*2' integer, 16 bits. 'int32' 'integer*4' integer, 32 bits. 'int64' 'integer*8' integer, 64 bits. 'uint8' 'integer*1' unsigned integer, 8 bits. 'uint16' 'integer*2' unsigned integer, 16 bits. 'uint32' 'integer*4' unsigned integer, 32 bits. 'uint64' 'integer*8' unsigned integer, 64 bits. 'single' 'real*4' floating point, 32 bits. 'float32' 'real*4' floating point, 32 bits. 'double' 'real*8' floating point, 64 bits. 'float64' 'real*8' floating point, 64 bits. The following platform dependent formats are also supported but they are not guaranteed to be the same size on all platforms. MATLAB C or Fortran Description 'char' 'char*1' character. 'short' 'short' integer, 16 bits. 'int' 'int' integer, 32 bits. 'long' 'long' integer, 32 or 64 bits. 'ushort' 'unsigned short' unsigned integer, 16 bits. 'uint' 'unsigned int' unsigned integer, 32 bits. 'ulong' 'unsigned long' unsigned integer, 32 bits or 64 bits. 'float' 'float' floating point, 32 bits. If the precision is 'char' or 'char*1', MATLAB reads characters using the encoding scheme associated with the file. See FOPEN for more information. The following formats map to an input stream of bits rather than bytes. 'bitN' signed integer, N bits (1<=N<=64). 'ubitN' unsigned integer, N bits (1<=N<=64). If the input stream is bytes and fread reaches the end of file (see FEOF) in the middle of reading the number of bytes required for an element, the partial result is ignored. However, if the input stream is bits, then the partial result is returned as the last value. If an error occurs before reaching the end of file, only full elements read up to that point are used. By default, numeric and character values are returned in class 'double' arrays. To return these values stored in classes other than double, create your PRECISION argument by first specifying your source format, then following it by '=>', and finally specifying your destination format. If the source and destination formats are the same then the following shorthand notation may be used: *source which means: source=>source For example, uint8=>uint8 read in unsigned 8-bit integers and save them in an unsigned 8-bit integer array *uint8 shorthand version of previous example bit4=>int8 read in signed 4-bit integers packed in bytes and save them in a signed 8-bit integer array (each 4-bit integer becomes one 8-bit integer) double=>real*4 read in doubles, convert and save as a 32-bit floating point array A = fread(FID,SIZE,PRECISION,SKIP) includes a SKIP argument that specifies the number of bytes to skip after each PRECISION value is read. If PRECISION specifies a bit source format, like 'bitN' or 'ubitN', the SKIP argument is interpreted as the number of bits to skip. The SIZE argument is optional when using this syntax. When SKIP is used, the PRECISION string may contain a positive integer repetition factor of the form 'N*' which prepends the source format of the PRECISION argument, like '40*uchar'. Note that 40*uchar for the PRECISION alone is equivalent to '40*uchar=>double', not '40*uchar=>uchar'. With SKIP specified, fread reads in, at most, a repetition factor number of values (default of 1), does a skip of input specified by the SKIP argument, reads in another block of values and does a skip of input, etc. until SIZE number of values have been read. If a SKIP argument is not specified, the repetition factor is ignored. Repetition with skip is useful for extracting data in noncontiguous fields from fixed length records. For example, s = fread(fid,120,'40*uchar=>uchar',8); reads in 120 characters in blocks of 40 each separated by 8 characters. A = fread(FID,SIZE,PRECISION,SKIP,MACHINEFORMAT) treats the data read as having a format given by the string MACHINEFORMAT. You can obtain the MACHINEFORMAT argument from the output of the FOPEN function. See FOPEN for possible values for MACHINEFORMAT. The SIZE and SKIP arguments are optional when using this syntax. [A, COUNT] = fread(...) Optional output argument COUNT returns the number of elements successfully read. Examples: The file alphabet.txt contains the 26 letters of the English alphabet, all capitalized. Open the file for read access with fopen, and read the first five elements into output c. Because a precision has not been specified, MATLAB uses the default precision of uchar, and the output is numeric: fid = fopen('alphabet.txt', 'r'); c = fread(fid, 5)' c = 65 66 67 68 69 fclose(fid); This time, specify that you want each element read as an unsigned 8-bit integer and output as a character. (Using a precision of 'char=>char' or '*char' will produce the same result): fid = fopen('alphabet.txt', 'r'); c = fread(fid, 5, 'uint8=>char')' c = ABCDE fclose(fid); See also fwrite, fseek, fscanf, fgetl, fgets, load, fopen, feof. Overloaded methods: serial/fread udp/fread i2c/fread icinterface/fread Reference page in Help browser doc fread help feof feof Test for end-of-file. ST = feof(FID) returns 1 if the end-of-file indicator for the file with file identifier FID has been set, and 0 otherwise. The end-of-file indicator is set when a read operation on the file associated with the FID attempts to read past the end of the file. See also ferror, fgetl, fgets, fread, fscanf, fopen. Reference page in Help browser doc feof read_file Read 10 characters Contents are random dat So far, we have read 10 characters Read 2 characters Contents are a! So far, we have read 12 characters clc read_file Read 10 characters Contents are random da So far, we have read 10 characters Read 10 characters Contents are ta! add m So far, we have read 20 characters Read 10 characters Contents are ore stuff So far, we have read 30 characters Read 10 characters Contents are 212r893yr So far, we have read 40 characters Read 10 characters Contents are 821y blksj So far, we have read 50 characters Read 10 characters Contents are dghle ueh So far, we have read 60 characters Read 1 characters Contents are So far, we have read 61 characters read_file Read 10 characters Contents are random da 1 : 10 2 : 114 3 : 97 4 : 110 5 : 100 6 : 111 7 : 109 8 : 32 9 : 100 10 : 97 So far, we have read 10 characters Read 10 characters Contents are ta! add m 1 : 116 2 : 97 3 : 33 4 : 10 5 : 10 6 : 97 7 : 100 8 : 100 9 : 32 10 : 109 So far, we have read 20 characters Read 10 characters Contents are ore stuff 1 : 111 2 : 114 3 : 101 4 : 32 5 : 115 6 : 116 7 : 117 8 : 102 9 : 102 10 : 10 So far, we have read 30 characters Read 10 characters Contents are 212r893yr 1 : 10 2 : 50 3 : 49 4 : 50 5 : 114 6 : 56 7 : 57 8 : 51 9 : 121 10 : 114 So far, we have read 40 characters Read 10 characters Contents are 821y blksj 1 : 56 2 : 50 3 : 49 4 : 121 5 : 32 6 : 98 7 : 108 8 : 107 9 : 115 10 : 106 So far, we have read 50 characters Read 10 characters Contents are dghle ueh 1 : 100 2 : 103 3 : 104 4 : 108 5 : 101 6 : 32 7 : 117 8 : 101 9 : 104 10 : 10 So far, we have read 60 characters Read 1 characters Contents are 1 : 10 So far, we have read 61 characters type read_file.m % % read_file.m % % -MCW Fall 2016 % fname = 'exampledata.z2'; myfile = fopen(fname, 'r'); if (myfile == -1) error(sprintf('File "%s" open did not work.', fname)); return end [mybuffer, num_read] = fread(myfile, 10, 'uchar'); while (num_read > 0) disp(sprintf('Read %d characters', num_read)); disp(sprintf('Contents are %s ', mybuffer)); for i=1:num_read disp(sprintf(' %d : %d ', i, mybuffer(i))); end disp(sprintf('So far, we have read %d characters', ftell(myfile))); [mybuffer, num_read] = fread(myfile, 10, 'uchar'); end if (fclose(myfile) == -1) error('File did not close'); end diary off