Octave Programming Tutorial/Saving and loading a MAT-file

From Wikibooks, open books for an open world
< Octave Programming Tutorial
Jump to: navigation, search

Octave Programming Tutorial

Chapter 1: A Tutorial Introduction

Chapter 2: Basic Octave Concepts

Saving and loading a MAT-file
Octave's Command Prompt

Chapter 3: Data Storage and Manipulation

Data Types and Operations on Point Values

Boolean and Rational
Strings
Portable Functions
Complex Numbers

Arrays and Matrices

What is an array?
Introduction to array operations
Vectors and basic vector operations
Struct Arrays
Cell Arrays
Sparce Matrices

Chapter 4: M File Programming

Scripts
Comments
The Input Function
Control Flow
Loops and Branches
Error Messages
Octave Caveats
Debugging M Files

Chapter 5: Graphics

Basic Graphics Commands
Annotating Plots

Chapter 6: Mathematical Manipulations

Linear Algebra

Simple matrix manipulation
More complicated matrix operations

Differential Equations

Ordinary Differential Equations
Partial Differential Equations

Chapter 7: Examples

Filtering
Controls

The Current Directory and Defined Path[edit]

It is necessary to declare a current directory before saving a file, loading a file, or running an M-file. By default, unless you edit the Octave shortcut, the current directory will be .../Octave/work. After you start Octave, change the current directory by either using the toolbar at the left-hand side of the screen, or entering the path in the bar at the top.

The current directory is the directory Octave will look in first for a function you try to call. Therefore if you have multiple folders and each of them has an M-file of the same name, there will not be a discrepancy if you set the current directory beforehand. The current directory is also the directory in which Octave will first look for a data file.

If you still want to call a function but it is not part of the current directory, you must define it using Octave's 'set path' utility. To access this utility, follow the path:

file > set path... > add folder...

You could also go to "add folder with subfolders...", if you're adding an entire group ,as you would if you were installing a toolbox. Then look for and select the folder you want. If you forget to do this and attempt to access a file that is not part of your defined path list, you will get an 'undefined function' error.

Saving Files[edit]

There are many ways to save to files in Octave.

  • save - saves data to files, *.mat by default
  • uisave - includes user interface
  • hgsave - saves figures to files, *.fig by default
  • diary [filename] - saves all the text input in the command window to a text file.

The files are saved in your current directory, as seen on the top of the window. By default the current directory is .../Octave/work.

Loading Files[edit]

Likewise, there are many ways to load files into the workspace

To load a saved .mat file, type:

>> load filename

The file must be in a recognized directory (usually your current directory, but at least one for which the path has been set).

The data in the .mat file is stored with the same name as the variable originally had when it was saved. To get the name of this and all other environment variables, type "who".

To open an .m file, you can use file -> open, or type

>>open filename

For instructions on how to open a data file, see Basic Reading and Writing data from a file.

File Naming Constraints[edit]

Octave for Windows retains the file naming constraints set by DOS. The following characters cannot be used in filenames:

  "  / : * < > | ? 

You're also not allowed to use the name of a reserved word as the name of a file.

Example:

while.m is not a valid file name because while is one of Octave's reserved words.


When you declare an m-file function, the m-file must be the same name as the function or Octave will not be able to run it. So for example if you declare a function 'factorial' as:

function Y = factorial(X)

You must save this as "factorial.m". Octave will name it for you if you save it after typing the function declaration, but if you change the name of the function you must change the name of the file manually, and vice versa.