Reading and Writing Data Files In Matlab MAT
Reading and Writing Data Files In Matlab
*. MAT Files • *. MAT files are binary Matlab files that can hold your data. • E. g. % Data to save x = [1 2; 3 4]; % Method 1: Binary *. mat files save x % saves the variable x in x. mat save My. Data x % saves the variable x in My. Data. mat save('My. Other. Data', 'x'); % saves the variable x in My. Other. Data. mat
Saving Multiple Variables in *. MAT Files % Data to save x = [1 2; 3 4]; y = 5; save My. Datas x y % saves the variables x & y in My. Datas. mat save('My. Other. Datas', 'x', 'y'); % saves the variable x & y in My. Other. Datas. mat
Loading Variables from *. MAT Files clear all load My. Data. mat whos load My. Other. Datas. mat whos d = load('My. Other. Datas. mat'); d
Pros and Cons of *. MAT Files Pros: • Convenient if the variables will be manipulated at a later time in Matlab. • Compact (saves memory). • Binary file format makes it difficult for a novice to accidentally view confidential data. Cons: • Not so great for sharing with colleagues who are not using Matlab. • Not editable with any other software.
xlswrite x = [1 2; 3 4]; % Data to be written File. Name = 'My. Excel. File. xlsx'; % Excel file name xlswrite(File. Name, x); % Write the data File. Name = 'My. Excel. File 2. xlsx'; % Specify that we want to write on Sheet 2 xlswrite(File. Name, x, 'Sheet 2'); File. Name = 'My. Excel. File 3. xlsx'; % Specify that we want to start our matrix on cell B, 2 xlswrite(File. Name, x, 'Sheet 1', 'B 2');
xlsread File. Name = 'My. Excel. File 3. xlsx'; [Numbers, Text, Raw] = xlsread(File. Name, 'Sheet 1', 'B 2: C 3')
Pros and Cons of Excel Files Pros: • Easy to share data with friends. • Data can be easily viewed and edited. • Data from people who like Excel can easily be imported into Matlab. Cons: • There’s nothing you can do in Excel that you can’t do in Matlab, but Matlab can do much more.
Custom Files File. Name = 'My. File. txt'; % Specify a file name fid = fopen(File. Name, 'w+'); % Open file for writing % Write to the file fprintf(fid, '%i %0. 2 f %sn', 5, 2. 7183, 'hello world'); % Close the file fclose(fid);
fopen • Permission may be any of the following: • Opens a file for reading or writing. • Usage: ‘r’ Open file for reading ‘w’ Open file for writing; discard existing contents • File. ID = fopen(File. Name, Permission) ‘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 • File. Name may be any file name with any extension (not just *. txt) • E. g. • File. Name = ‘My. File. dat’; • File. Name = ‘My. File. m’;
fprintf • Prints data to a file. • Usage: • fprintf(File. ID, Format, Data); • File. ID is the file identifier returned by fopen. • Format specifies the data formatting and may include any of the following: Format Specifier Used For ‘%a. bf’ Floating point digits with a digits before the decimal place and b digits after (e. g. ‘%1. 2 f’ = 3. 14) ‘%s’ Strings (e. g. ‘hello world’) ‘%i’ Integers (e. g. 5) ‘b’ Backspace ‘n’ New line ‘t’ Horizontal tab ‘’’’ Single quotation mark ‘%%’ Single % character ‘\’ Single character
fprintf (continued) • If File. ID = 1, then fprintf prints to the command window. The string we will print The integer we will print • E. g. • fprintf(1, '%s %in', 'Display a number', 5) File identifier The string will be followed by an integer, and then a carriage return We’re writing a string ØDisplay a number 5
fclose • Closes the file. • Usage: • fclose(File. ID)
File Reading File. Name = 'My. File. txt'; % Specify a file name % Open the file for reading fid = fopen(File. Name, 'r'); % Display the contents of the file while true This. Line = fgetl(fid); % Get the next line of the file and return it as a string if ~ischar(This. Line) break end disp(This. Line) end % Close the file fclose(fid);
fgetl • Gets a line from a file • Usage: • The. Line = fgetl(File. ID) • Where File. ID is the file identifier returned by fopen. • The. Line is the next unread line of the file excluding the line terminator (e. g. excluding n). • Repeated calls to fgetl result in each successive line of the file being read until the end of the file is reached, at which time The. Line = -1.
Saving Data % Open a file for writing fid = fopen('My. Data. File. dat', 'a+'); % Write a comment fprintf(fid, '%% Subject. Name = ''%s''; n', 'Harry Potter'); fprintf(fid, '%% Age = %i; n', 11); % Write some data Data = rand(5, 2); fprintf(fid, '%0. 2 fn', Data'); % Close the data file fclose(fid);
Loading the Data in Matlab % Load the data Saved. Data = load('My. Data. File. dat') Comments are ignored Only the data is loaded into Matlab
Evaluating the Comments File. Name = 'My. Data. File. dat'; fid = fopen(File. Name, 'r'); while true This. Line = fgetl(fid); if ischar(This. Line) && ~isempty(This. Line) if This. Line(1)=='%' eval(['Vars. ', This. Line(3: end)]); end else break end fclose(fid);
eval • Evaluates a string and makes the evaluated variables available in the current script or function. • E. g. • eval(‘x = 1; ’) • Creates a variable x and sets it equal to 1. • eval('Subject. Name = ''Harry Potter'''); • Creates a variable called Subject. Name and sets it equal to ‘Harry Potter’.
Pros and Cons of Custom File Formats Pros: • New file types can be created that anyone snooping around your computer will not know how to open. • This keeps subjects data a little safer. • Data can be easily loaded or read back in via Matlab • Files may be saved in *. txt format if you want other people to be able to read and edit them. Cons: • The syntax is more complicated than for the other data saving methods.
- Slides: 20