Jump to navigation Jump to search


343 bytes added, 06:32, 28 July 2017
→‎Octave usage: Overhaul section. Make use of Template:Manual and syntax highlighting.
==How do I execute an Octave script?==
First of all, make sure you understand [ 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 <code>.m </code> extension. Thus, if you have a script called <code>foo.m</code>, just type <code>foo</code> from within the Octave command prompt to execute it. You have to make sure that the script is in your current working directory or in Octave's load path. Type <code>path</code> in Octave to see what this path is, and type <code>{{manual|pwd</code> }} to print get the current working directory (where youor type {{manual|path}} to see which paths belong to Octave're currently standing)s load path. The current working directory is referred to as "." in the <code>output of {{manual|path</code>}}.
If the script name has characters that are not valid for an Octave identifier, or if you do not want to use {{manual|addpath }} to add the script's location to the current path, you can use the <code>{{manual|run</code> }} function instead:
<syntaxhighlight lang="octave"> run("Script Name With Spaces.m") octave> run("/opt/local/foo.m")</syntaxhighlight>
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>{{manual|argv</code> }} command:
$ octave the-script.m arg1 arg2
If you call the script from the shell and it's plotting, please note [[#When I try plotting from a script, why am I not seeing anything?|how to plot when running a script from the shell]].
==How do I erase close a figure?==
closeplot(); closefig(number)To close the current figure type {{manual|close}} in the Octave command prompt.
==How do I set the number of displayed decimals?==
octave:1> format long octave:2> pi pi = 3.14159265358979 octave:3> You can control the number of displayed decimals using the {{manual|format short octave}} command:4> pi pi = 3.1416
<syntaxhighlight lang==How do I call an "octave function from C++?">>> format long>> pipi =3.14159265358979>> format short>> pipi =3.1416</syntaxhighlight>
* Here is ==How do I call an untested code snippet for calling rand([9000,1]), modified Octave function from a post by Herber FarnsworthC++? to help-octave on 2003-05-01:==
#include <octavePlease read the manual https://oct.h> ColumnVector NumRands(2); NumRands(0) = 9000; NumRands(1) = 1; octave_value_list f_arg, f_ret; f_arg(0) = octave_value(NumRands); f_ret = feval("rand",f_arg,1); Matrix unis(f_ret(0)html.matrix_value());
==How do I change color/line definition in gnuplot postscript?==
==How do I tell if a file exists?==
Look at functions like One can use the function {{manual|exist}} to tell if a regular file, file_in_pathsay <code>foo.. and txt</code> exist in Octave's load path, or the other functions that their descriptions point tocurrent directory: <syntaxhighlight lang="octave">>> exist ("foo.txt", "file") # 2, if file exists, 0 otherwiseans = 2</syntaxhighlight>
==How do I create a plot without a window popping up (plot to a file directly)?==
'''This only works with gnuplot as graphics_toolkit, NOT with fltk. See [ Bug#33180]'''<syntaxhighlight lang="octave">  figure(1, "visible", "off"); plot(sin(1:100)); print -deps "/tmp/sin.eps" One can set that behaviour as default:</syntaxhighlight>
One can set(0, 'defaultfigurevisible', 'off');that behavior as default:
<syntaxhighlight lang==How do I make Octave use more precision?=="octave">set (0, "defaultfigurevisible", "off");</syntaxhighlight>
==How do I increase Octave's default numerical type is IEEE 754 doubles, a.k.a. hardware floats. This type has 52 bits of precision or about 16 decimal digits. It's implemented in your computer's hardware, in your CPU, so it's '''fast'''. This type is assumed throughout for Octave's calculations.?==
You can use Octave's default numerical type is [ IEEE 754] binary64 , a.k.a few other built-in types. The int64 "double" or "hardware floats". This type will have 63 has a precision of 53 bits of precisionor about 16 decimal digits. It is supported by each modern computer hardware, so it is really '''fast'''. One bit This type is used assumed throughout for the sign, but if you donOctave't want to lose that bits calculations. If more precision was required, uint64 one can be used instead. These obtain a "few bits more" by using integer types, howevere.g. {{manual|uint64}}, but in general one cannot represent numbers as large as the default double type, and can only represent integersexpect more precision from any '''fast''' numerical software. Furthermore, there is no way Just to represent integer literalsvisualize "how big" those numerical limits are, so if you do consider the following table:
{| class="wikitable"|+ Limits of some of Octave's data types obtained by {{manual|intmax}} and {{manual|flintmax}}.|-| <code>intmax ("uint64")</code>| style="text-align: right;" | <code>18,446,744,073,709,551,615</code>| <code>2^64-1</code>|-| <code>intmax ("int64")</code>| style="text-align: right;" | <code>9,223,372,036,854,775,807</code>| <code>2^63-1</code>|-| <code>flintmax ("double")<syntaxhighlight lang/code>| style="Octavetext-align: right;"| <code>9,007,199,254,740,992</code>| <code>2^53</code>uint64|-| <code>flintmax (18446744073709551610"single")</code>| style="text-align: right;" | <code>16,777,216</code>| <code>2^24</syntaxhighlightcode>|}
the literal When working with other types than "18446744073709551610double" first gets converted in Octave, one has to a double precision typemake sure, so <code>uint64</code>'s additional precision that the first operand is lost. Instead, initialize the <code>uint64</code> with smaller numbers and perform a computation converted to get the larger number you want. E.g., desired type:
<syntaxhighlight lang="Octave">>>uint64(9999999999999992^53 + 1) * 10000ans = 9007199254740992>> uint64 (2^53) + 1ans = 9007199254740993</syntaxhighlight>
would produce value 9999999999999990000Notice the difference, which in the first line the addition within the brackets is close performed using double precision, therefore the result gets "truncated" to the maximum possible value for the without a warning. The third line uses throughout uint64 type, but can't be at the moment input directly, doing uint64(9999999999999990000), due to the mentioned error of roundingprecision.
Alternatively, one may use arbitrary Consider carefully if your problem really needs more precision arithmetic, which has as much . Often if you're running out of precision as is practical to hold the problem lies fundamentally in your computer's memorymethods being [https://en. The ''symbolic'' package has a vpa() function for arbitrary precision arithmeticwikipedia. Note that arbitrary org/wiki/Numerical_stability numerically unstable], thus more precision arithmetic must be implemented '''in software''' which makes it much slower than hardware floatswill not help you here.
Consider carefully if your problem really needs If you absolutely must have more precision. Often if , you're running out of precision the problem lies fundamentally in your methods being at present better off using a [http numerically unstableComputer_algebra_system CAS]instead of Octave. However, so more precision will not help you here. If you absolutely CAS or symbolic computations must use arbitrary-precision arithmetic, yoube implemented '''in software'''re at present better off using a CAS instead of Octavewhich makes it much slower than hardware floats. An example of such a CAS is [ / Sage] or have a look at Octave's [[Symbolic package]].
==How do I run a Matlab P-file in Octave?==
You can't. Matlab P-files (files with a <code>.p </code> file extension), also known as P-code, are [ obfuscated] files than cannot be run outside of Matlab itself. The original source Matlab m-files that were used to generate the these P-files should be used in Octave instead.
There are no plans to support running P-files produced by Matlab in Octave.
==How does Octave solve linear systems?==
In addition to consulting Octave's source for the precise details, you can read the Octave manual for a complete high-level description of the algorithm that Octave uses to decide how to solve a particular linear system, e.g. how the backslash operator {{Codeline|<code>A\x}} </code> will be interpreted. Sections [ Techniques Used for Linear Algebra] and [ Linear Algebra on Sparse Matrices] from the manual describe this procedure.
==How do I do X?==
You are probably looking for the function ''{{manual|lookfor''}}. This function searches the help text of all functions for a specific string and returns a list of functions. Note that by default it will only search the first line of the help text (check ''<code>help lookfor'' </code> at the octave prompt for more). The following example helps to find the function to calculate correlation coefficient in a matrix:
octave>> lookfor correlation corr2 Returns the correlation coefficient between I and J. cor corr Compute matrix of correlationcoefficients. corrcoef Compute a matrix of correlationcoefficients. spearman Compute Spearman's rank correlation coefficient RHO for each of the variables sp autocor Return the autocorrelations from lag 0 to H of vector X.
Also, there's a high chance that the function name has a suggestive name, and so you can try auto-completion to get some hints. For the previous example, typing ''<code>corr'' </code> at the octave promp prompt followed by pressing [the "Tab] "-Key twice would suggest the following:
octave>> corr
corr2 corrcoef

Navigation menu