FAQ: Difference between revisions

Jump to navigation Jump to search
1,636 bytes added ,  16 November 2012
Line 378: Line 378:


=How do I...?=
=How do I...?=
==How do I execute an Octave script?==
First of all, make sure you understand [http://www.gnu.org/software/octave/doc/interpreter/Script-Files.html the difference between script files and function files]. If you want to execute a function defined in a file, just call the function like any other Octave function: <code>foo(arg1, arg2);</code>
To execute a script from within Octave, just type its name without the .m extension. So, if you have a script called <code>foo.m</code>, just type <code>foo</code> from within Octave to execute. You have to make sure that the script is in your current path. Type <code>path</code> in Octave to see what this path is.
If the script name has characters that are not valid for an Octave identifier, or if you do not want to use addpath to add the script's location to the current path, you can use the run function instead:
  octave> run("Script Name With Spaces.m")
  octave> run("/opt/local/foo.m")
An alternative is to run the script from outside Octave by calling Octave from your operating system shell. Unlike calling the script from inside Octave, this also allows you to pass arguments from the shell into the script, which the script can access using the <code>argv</code>:
  $ octave the-script.m arg1 arg2
In a Unix environment, if the script has a [http://en.wikipedia.org/wiki/Shebang_%28Unix%29 shebang] (e.g. <code>#!/usr/bin/octave) and executable permissions, you can call it like any other Unix program with arguments:
    ./the-script arg1 arg2
If you call the script from the shell and it's plotting, please note how to plot when running a script from the shell.


==do xxxx?==
==do xxxx?==

Navigation menu