In Matlab MAT Files MAT files are binary Matlab files that can hold your data Eg Data to save x 1 2 3 4 Method 1 Binary mat files save x saves the variable x in ID: 583789
Download Presentation The PPT/PDF document "Reading and Writing Data Files" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
Reading and Writing Data Files
In MatlabSlide2
*.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
MyData
x
% saves the variable x in
MyData.mat
save(
'
MyOtherData
'
,
'x'
);
% saves the variable x in
MyOtherData.matSlide3
Saving Multiple Variables in *.MAT Files
% Data to save
x = [1 2; 3 4];
y = 5;
save
MyDatas
x
y
% saves the variables x & y in
MyDatas.mat
save(
'
MyOtherDatas
'
,
'
x'
,
'y
'
);
% saves the variable x & y in
MyOtherDatas.matSlide4
Loading Variables from *.MAT Files
clear
all
load
MyData.mat
whos
load
MyOtherDatas.mat
whos
d = load(
'
MyOtherDatas.mat
'
);
dSlide5
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.Slide6
xlswrite
x = [1 2; 3 4];
% Data to be written
FileName
=
'MyExcelFile.xlsx'
;
% Excel file name
xlswrite
(
FileName,x
);
% Write the data
FileName
=
'MyExcelFile2.xlsx'
;
% Specify that
we
want to write on Sheet 2
xlswrite
(FileName,x,
'Sheet2'
);
FileName
=
'MyExcelFile3.xlsx'
;
% Specify that we want to start our matrix
on cell B,2
xlswrite
(FileName,x,
'Sheet1'
,
'B2'
);Slide7
xlsread
FileName
=
'MyExcelFile3.xlsx'
;
[
Numbers,Text,Raw
] =
xlsread
(FileName,
'Sheet1'
,
'B2:C3'
)Slide8
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.Slide9
Custom Files
FileName
=
'MyFile.txt'
;
% Specify a file name
fid =
fopen
(
FileName
,
'w+'
);
% Open file for writing
% Write to the filefprintf(fid,
'%i
%0.2f %s\n',5,2.7183,'hello world'
); % Close the file
fclose(fid);Slide10
fopen
Opens a file for reading or writing.
Usage:
FileID
=
fopen
(
FileName,Permission
)
FileName
may be any file name with any extension (not just *.txt)
E.g.
FileName
= ‘MyFile.dat’;
FileName = ‘MyFile.m’;Permission may be any of the following:‘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 flushingSlide11
fprintf
Prints data to a file.
Usage:
fprintf
(
FileID,Format,Data
);
FileID
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.2f’ = 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 \ characterSlide12
fprintf (continued)
If
FileID
= 1, then
fprintf
prints to the command window.
E.g.
fprintf
(1,
'%s %
i
\
n'
,
'Display a number',5)Display a number 5
File identifier
We’re writing a string
The string will be followed by an integer, and then a carriage return
The string we will print
The integer we will printSlide13
fclose
Closes the file.
Usage:
fclose
(
FileID
)Slide14
File Reading
FileName
=
'MyFile.txt'
;
% Specify a file name
%
Open the file for reading
fid =
fopen
(
FileName
,
'r');
% Display the contents of the file
while true ThisLine
= fgetl(fid
); %
Get the next line of the file and return it as a string
if
~
ischar
(
ThisLine
)
break
end
disp
(
ThisLine
)
end
% Close the file
fclose
(fid);Slide15
fgetl
Gets a line from a file
Usage:
TheLine
=
fgetl
(
FileID
)
Where
FileID
is the file identifier returned by
fopen
.
TheLine 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 TheLine = -1.Slide16
Saving Data
% Open a file for writing
fid =
fopen
(
'MyDataFile.
dat
'
,
'a+'
);
% Write a comment
fprintf
(fid,'%% SubjectName = ''%s'';\
n'
,'Harry Potter');
fprintf(fid,
'%% Age = %i;\n'
,11);
% Write some dataData = rand(5,2);
fprintf
(fid,
'%0.2f %0.2f\
n'
,Data
');
% Close the data file
fclose
(fid);Slide17
Loading the Data in Matlab
% Load the data
SavedData
= load(
'MyDataFile.dat'
)
Comments are ignored
Only the data is loaded into MatlabSlide18
Evaluating the Comments
FileName
=
'MyDataFile.dat'
;
fid
=
fopen
(
FileName
,
'r'
);
while true
ThisLine =
fgetl(fid);
if ischar
(ThisLine) && ~
isempty(ThisLine
)
if
ThisLine
(1)==
'%'
eval
([
'
Vars
.'
,
ThisLine
(3:end)]);
end
else
break
end
end
fclose
(fid);Slide19
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
(
'
SubjectName
= ''Harry Potter'''
);
Creates a variable called SubjectName and sets it equal to ‘Harry Potter’.Slide20
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.