Difference between revisions of "Matlab IO"

From Sutherland_wiki
Jump to: navigation, search
m
Line 1: Line 1:
{{Stub|page}}
+
== Input in MATLAB ==
 +
Input can be accomplished in several ways in Matlab:
  
== Terminal and File Input in MATLAB ==
+
* Interactive input from the user.  This is accomplished using the [[#The_INPUT_command|<tt>input</tt>]] command.
  
=== INPUT ===
+
* Reading files from disk.  There are two common methods to do this: using [[#The_FSCANF_command|<tt>fscanf</tt>]] command and using the [[#Loading_variables_using_LOAD|load]] command.
  
=== FSCANF ===
+
=== The INPUT command ===
 +
The basic syntax for the <tt>input</tt> command is:
 +
{| border="0" cellpadding="10" cellspacing="0"
 +
|-
 +
|<source lang="matlab">
 +
x = input('Enter the value of x: ');
 +
</source>
 +
|}
 +
This outputs the given text to the command window.  The user can then enter a value and it will be stored in <tt>x</tt>.
  
==== File Input using FSCANF ====
 
  
 
=== Loading variables using LOAD ===
 
=== Loading variables using LOAD ===
 +
{{Stub|section}}
 +
 +
 +
 +
=== The FSCANF command ===
 +
 +
There are three steps to read from a file:
 +
# Open the file using <tt>'''fopen'''</tt>
 +
# Read from the file using <tt>'''fscanf'''</tt>
 +
# Close the file using <tt>'''fclose'''</tt>
 +
 +
The <tt>fscanf</tt> command is used primarily for reading from files.  The basic syntax is:
 +
{| cellpadding="10" style="background-color:cornsilk"
 +
|-
 +
|<source lang="matlab">
 +
value = fscanf( fid, format, size );
 +
</source>
 +
|-
 +
|
 +
{| border="1" cellpadding="5" cellspacing="0" style="background-color:cornsilk"
 +
  |- valign="top"
 +
  | '''fid'''    || The identifier for the file to read from.  This is obtained using the [[#Opening_and_Closing_Files|<tt>fopen</tt>]] command.
 +
  |- valign="top"
 +
  | '''format''' || A text string indicating the format to use when reading values from the file.  This is of the form <tt>%Na</tt>, where <tt>N</tt> specifies the width to read and <tt>f</tt> specifies the format to read.
 +
  {| cellpadding="5" style="background-color:Cornsilk"
 +
  |-
 +
  | '''<tt>%c</tt>''' || Reads a character (e.g. "q")
 +
  |-
 +
  | '''<tt>%s</tt>''' || Reads a character string (e.g. "quiet")
 +
  |-
 +
  | '''<tt>%d</tt>''' || Reads an integer (e.g. 12345)
 +
  |-
 +
  | '''<tt>%f</tt>''' || Reads a floating point number (e.g. 5.62)
 +
  |}
 +
  |- valign="top"
 +
  | '''size'''  || OPTIONAL - sets a maximum limit on the number of entries to be read from the file.  If omitted, then all of the file may be read.  Valid specifications are:
 +
* <tt>N</tt> - read at most <tt>N</tt> entries from the file.
 +
* <tt>inf</tt> - read at most to the end of the file.
 +
* <tt>[M,N]</tt> - read <tt>M*N</tt> entries from the file and arrange them into an <tt>M x N</tt> matrix.
 +
  |}
 +
|}
 +
 +
 +
==== Opening and Closing Files ====
 +
 +
Before reading from a file, we must open it.  This is done using the <tt>fopen</tt> command:
 +
{| cellpadding="10"
 +
|-
 +
|<source lang="matlab">
 +
fid = fopen('filename','r');  % opens a file named "filename" for reading.
 +
</source>
 +
|}
 +
This assigns an identifier to <tt>fid</tt> that can be used in subsequent file operations (reading, closing, etc.).  The <tt>'r'</tt> indicates that this file is to be read from.  Options are:
 +
{| border="1" cellpadding="5" cellspacing="0"
 +
|+ '''Flags for <tt>fopen</tt>'''
 +
|-
 +
! Flag !! Description
 +
|-
 +
| '''<tt>'r'</tt>''' || Open a file for reading.
 +
|-
 +
| '''<tt>'w'</tt>''' || Open a file for writing (creates the file if it doesn't already exist).  An existing file will be overwritten.
 +
|-
 +
| '''<tt>'a'</tt>''' || Open a file and append it (creates the file if it doesn't already exist).
 +
|}
 +
 +
After you are done with a file, you should close it using the <tt>fclose</tt> command:
 +
{| cellpadding="10"
 +
|-
 +
|<source lang="matlab">
 +
fclose(fid);  % closes the file referred to be "fid"
 +
</source>
 +
|}
 +
 +
 +
==== A Simple Example ====
 +
{| border="1" cellpadding="5" cellspacing="0"
 +
|-
 +
! File "ages.dat" !! Matlab Commands !! Explanation
 +
|- valign="top"
 +
| <pre>Fred 64
 +
Bob  32
 +
Julia  49</pre>
 +
|<source lang="matlab">
 +
file = fopen('ages.dat','r');
 +
name = fscanf( file, '%s', 1 );
 +
age  = fscanf( file, '%s' );
 +
fclose(file);
 +
</source>
 +
| Reads a string number into the variable <tt>name</tt>. Note the "1" at the end of this statement.  If we didn't use "1" when reading the name, then it would have read the entire file into the string.  After reading the name, we read the age into the variable <tt>age</tt>.
 +
|}
 +
 +
If we wanted to read the whole file, we would need a few additional things:
 +
* A [[Matlab_Arrays#Cell_Arrays|cell array]] to hold the names
 +
* An [[Matlab_Arrays|array]] to hold the ages.
 +
* A way to determine when we hit the end of the file.  This is done using the <tt>'''feof'''(fid)</tt> command.
 +
Using these concepts, we can now read the file.
 +
{| cellpadding="10"
 +
|-
 +
|<source lang="matlab">
 +
fid = fopen('ages.dat','r');      % opens the file "ages.dat" for reading.
 +
i = 1;
 +
names = {};
 +
ages = [];
 +
while ~feof(fid)
 +
  names{i} = fscanf(fid,'%s',1);  % read the name.
 +
  age(i)  = fscanf(fid,'%i');    % read the age.
 +
  i = i+1;                        % get ready to read the next line
 +
end
 +
fclose(fid);                      % close the file.
 +
</source>
 +
|}
  
  
  
 
== Terminal and File Output in MATLAB ==
 
== Terminal and File Output in MATLAB ==
 +
{{Stub|section}}
 +
 
=== DISP ===
 
=== DISP ===
 
=== FPRINTF ===
 
=== FPRINTF ===
Line 22: Line 143:
  
 
== Exporting Matlab Figures ==
 
== Exporting Matlab Figures ==
 +
{{Stub|section}}

Revision as of 19:37, 19 September 2008

Input in MATLAB

Input can be accomplished in several ways in Matlab:

  • Interactive input from the user. This is accomplished using the input command.
  • Reading files from disk. There are two common methods to do this: using fscanf command and using the load command.

The INPUT command

The basic syntax for the input command is:

x = input('Enter the value of x: ');

This outputs the given text to the command window. The user can then enter a value and it will be stored in x.


Loading variables using LOAD

Warn.jpg
This section is a stub and needs to be expanded.
If you can provide information or finish this section you're welcome to do so and then remove this message afterwards.


The FSCANF command

There are three steps to read from a file:

  1. Open the file using fopen
  2. Read from the file using fscanf
  3. Close the file using fclose

The fscanf command is used primarily for reading from files. The basic syntax is:

value = fscanf( fid, format, size );
fid The identifier for the file to read from. This is obtained using the fopen command.
format A text string indicating the format to use when reading values from the file. This is of the form %Na, where N specifies the width to read and f specifies the format to read.
%c Reads a character (e.g. "q")
%s Reads a character string (e.g. "quiet")
%d Reads an integer (e.g. 12345)
%f Reads a floating point number (e.g. 5.62)
size OPTIONAL - sets a maximum limit on the number of entries to be read from the file. If omitted, then all of the file may be read. Valid specifications are:
  • N - read at most N entries from the file.
  • inf - read at most to the end of the file.
  • [M,N] - read M*N entries from the file and arrange them into an M x N matrix.


Opening and Closing Files

Before reading from a file, we must open it. This is done using the fopen command:

fid = fopen('filename','r');  % opens a file named "filename" for reading.

This assigns an identifier to fid that can be used in subsequent file operations (reading, closing, etc.). The 'r' indicates that this file is to be read from. Options are:

Flags for fopen
Flag Description
'r' Open a file for reading.
'w' Open a file for writing (creates the file if it doesn't already exist). An existing file will be overwritten.
'a' Open a file and append it (creates the file if it doesn't already exist).

After you are done with a file, you should close it using the fclose command:

fclose(fid);  % closes the file referred to be "fid"


A Simple Example

File "ages.dat" Matlab Commands Explanation
Fred 64
Bob   32
Julia  49
file = fopen('ages.dat','r');
name = fscanf( file, '%s', 1 );
age  = fscanf( file, '%s' );
fclose(file);
Reads a string number into the variable name. Note the "1" at the end of this statement. If we didn't use "1" when reading the name, then it would have read the entire file into the string. After reading the name, we read the age into the variable age.

If we wanted to read the whole file, we would need a few additional things:

  • A cell array to hold the names
  • An array to hold the ages.
  • A way to determine when we hit the end of the file. This is done using the feof(fid) command.

Using these concepts, we can now read the file.

fid = fopen('ages.dat','r');      % opens the file "ages.dat" for reading.
i = 1;
names = {};
ages = [];
while ~feof(fid)
  names{i} = fscanf(fid,'%s',1);  % read the name.
  age(i)   = fscanf(fid,'%i');    % read the age.
  i = i+1;                        % get ready to read the next line
end
fclose(fid);                      % close the file.


Terminal and File Output in MATLAB

Warn.jpg
This section is a stub and needs to be expanded.
If you can provide information or finish this section you're welcome to do so and then remove this message afterwards.

DISP

FPRINTF

File Output

Saving Variables with SAVE

Exporting Matlab Figures

Warn.jpg
This section is a stub and needs to be expanded.
If you can provide information or finish this section you're welcome to do so and then remove this message afterwards.