Documentation:Math Department Teaching Resources/OctaveTips

From UBC Wiki

How do I run the posted M-files?

Download and save the .m files in your code folder. In MATLAB/Octave, make sure that your present working directory is the folder in which you saved the .m files. (You can check this using the pwd command.) At the MATLAB/Octave prompt type the name of the m-file you wish to run (without the .m extension), for example if you would like to run the lagrange.m file, type lagrange at the prompt to see the desired result.

Now, if your M-file is a function, that is, it starts with a function declaration statement like

function variable=functionname(argument1,argument2)

then you first have to specify the arguments before running it. For example, plotspline.m is a function file which takes arguments X and Y, the vectors of x and y coordinates. To run plotspline.m, first specify the X and Y vectors at the command prompt, then run the desired function.

>>X=[1 2 3 4];

>>Y=[3 8 5 7];

>>plotspline(X,Y)

You can also specify the arguments when calling the function:

>>plotspline([1 2 3 4],[3 8 5 7])


How do I use the in-built help in MATLAB/Octave?

You can ask both MATLAB and Octave about the meaning of any predefined function by using the help command. For example, suppose you would like to know more about the norm function in MATLAB/Octave. Type help norm at the MATLAB/Octave prompt to get information about the syntax and much more.


I am getting error messages when using the plot command in Octave. What should I do?

Usually, the Windows and Mac Octave installers should automatically install Gnuplot, the plotting engine for Octave. But if you are getting error messages, Gnuplot is most probably not properly installed. Try downloading and installing Gnuplot using the link below and then run your plot commands in Octave.

GNUPLOT download

For Mac users, see also here.

How do I print out the output from the MATLAB/Octave command prompt?

You can print out all the commands and output at the command prompt using the diary command as follows

>>diary filename

>>some commands

   some output

>>diary off

This will save all your commands and output in a text file in your code folder.

For example, try this

>>diary mycommands

>>A=[1 2 3; 4 5 6]

A=

1 2 3

4 5 6

>>diary off

You should now have a file 'mycommands.txt' in your working directory with the above command and output. You can print it off from there.


How do I open .mat files in MATLAB/Octave?

You can use the load command to open .mat files.

For the Spectrogram assignment, type load testsig.mat at the command prompt. Then if you type y, you should see the signal vector, and if you type Fs, you should see the frequency.


How do I change the appearance of the command prompt in Octave?

In Octave, the default command prompt in Windows can be annoyingly long. To change it to just the number of the command followed by the prompt ">", type

>>PS1("\\#>")

For more information on the PS1 command, type

>>help PS1


How do I print a plot from MATLAB/Octave?

Add caption here

One way to do this is to first print the plot to a file.

To produce an pdf file testplot.pdf containing the current plot window you would type

>>print testplot.pdf

Similarly, you could produce jpeg (testplot.jpg) or postscript (testplot.eps) file. The files are saved in your code folder.

Now open the file in your favorite program that supports the chosen format and print it. For example, you could use Acrobat reader to print pdf files.

Otherwise, if you would like to copy the figure to a word processor and then print it, go to the figure window and select the icon corresponding to the "Copy the plot to the clipboard" command, as shown in the image below. You can then paste the plot to your favorite word processor.

I'm using Octave on a Mac and can't see the plots. What should I do?

All of these things need to be installed to get plots:

  1. Octave
  2. Gnuplot
  3. Aquaterm (first google result gives the download) to actually render the graphs
  4. X11 window system (available on the OS X install disk) because Gnuplot doesn't seem to be happy without it. I had this installed anyway so I'm not sure if it's strictly necessary but everything I found by searching online suggested it was necessary.

With all 4 things installed things should go smoothly. I think gnuplot defaults to terminal type 'aqua', but if for some reason it doesn't, then that also needs to be set. Instructions on how to set terminal type to 'aqua', see the Mac section on the installation instructions page.

How do I change directory in Octave?

By default, Octave will look for your codes in the present working directory. On Windows, the present working directory is somewhere under the install directory, possibly the folder C:\Program Files\Octave. You can check this using the present working directory ’pwd’ command as follows:

octave-3.0.3.exe:1>pwd

ans=C:\Program Files\Octave

On a Mac, you might get:

octave-3.2.3:1> pwd

ans = /Users/[your username]

because when you open Octave on a Mac, it sets the working directory to be your home directory.

It’s a good idea to create a new folder in which you can save all your code files. For example, create the folder "M Octave Codes" in "My Documents" and make this new folder the current working directory by using the change directory command cd as follows:

octave-3.0.3.exe:2>cd "C:/Documents and Settings/My Documents/My Octave Codes"

octave-3.0.3.exe:3>pwd

ans=C:\Documents and Settings\My Documents\My Octave Codes

Note:

  • When typing in the path, you have to use the forward slash character "/" instead of the backslash "\" that you would typically use in a Windows Command Prompt.
  • In the above example, you have to put the path in quotes so that Octave parses the spaces correctly. If your path has no spaces in it, then you can omit the quotation marks.
  • The character ~ is shorthand for your home directory, just as it is in Unix shells.

An alternative way to change the working directory is by editing the properties of the Octave Desktop Icon. Right click the Octave Desktop Icon, select "Properties" and then edit the "Start In:" folder to be your code folder. When you start Octave, the working directory will automatically be the folder you specified in the "Start In:" field.

I want to write a M-file, what text editor should I use?

An M-file is just a text file, so you can use any text editor to write an M-file. Octave comes with a buit-in text editor. The default text editor in Octave is the SciTE program. To open the editor, just type the command ’edit’ at the prompt.

octave-3.0.3.exe:1>edit

The editor opens in a new window.

You can type up commands you want Octave to run in the Editor and save them in an M-file so that you do not have to type them again if you need to rerun the commands. For example, let’s create the following simple script file to plot a sine wave. Type the following commands in the editor:

%Script file to plot a sine wave

x=0:0.01:2*pi;

plot(x, sin(x));

xlabel(’x’);

ylabel(’y=sin x’);

Then select File>Save As and save the file as script1.m in your code folder. If you don't know which is your code folder, make sure you read the FAQ #9 above.

To run the M-file, go to the Octave prompt and type

octave-3.0.3.exe:2>script1

You should see a figure window with the sine plot.