Octave – funzioni e scripts V – 49

archimedes

Oggi copio qui continuando da qui.

Files di funzioni

Except for simple one-shot programs, it is not practical to have to define all the functions you need each time you need them. Instead, you will normally want to save them in a file so that you can easily edit them, and save them for use at a later time.
Octave does not require you to load function definitions from files before using them. You simply need to put the function definitions in a place where Octave can find them.
When Octave encounters an identifier that is undefined, it first looks for variables or functions that are already compiled and currently listed in its symbol table. If it fails to find a definition there, it searches a list of directories (the path) for files ending in .m that have the same base name as the undefined identifier. (The ‘.m’ suffix was chosen for compatibility with MATLAB). Once Octave finds a file with a name that matches, the contents of the file are read. If it defines a single function, it is compiled and executed. See Script Files, for more information about how you can define more than one function in a single file.
When Octave defines a function from a function file, it saves the full name of the file it read and the time stamp on the file. If the time stamp on the file changes, Octave may reload the file. When Octave is running interactively, time stamp checking normally happens at most once each time Octave prints the prompt. Searching for new function definitions also occurs if the current working directory changes.
Checking the time stamp allows you to edit the definition of a function while Octave is running, and automatically use the new function definition without having to restart your Octave session.
To avoid degrading performance unnecessarily by checking the time stamps on functions that are not likely to change, Octave assumes that function files in the directory tree octave-home/share/octave/version/m will not change, so it doesn’t have to check their time stamps every time the functions defined in those files are used. This is normally a very good assumption and provides a significant improvement in performance for the function files that are distributed with Octave.
If you know that your own function files will not change while you are running Octave, you can improve performance by calling ignore_function_time_stamp ("all"), so that Octave will ignore the time stamps for all function files. Passing "system" to this function resets the default behavior.

Command: edit name
Command: edit field value
Command: value = edit get field

Edit the named function, or change editor settings.
If edit is called with the name of a file or function as its argument it will be opened in the text editor defined by EDITOR.

If the function name is available in a file on your path and that file is modifiable, then it will be edited in place. If it is a system function, then it will first be copied to the directory HOME (see below) and then edited. If no file is found, then the m-file variant, ending with “.m“, will be considered. If still no file is found, then variants with a leading “@” and then with both a leading “@” and trailing “.m” will be considered.
If name is the name of a function defined in the interpreter but not in an m-file, then an m-file will be created in HOME to contain that function along with its current definition.
If name.cc is specified, then it will search for name.cc in the path and try to modify it, otherwise it will create a new .cc file in the current directory. If name happens to be an m-file or interpreter defined function, then the text of that function will be inserted into the .cc file as a comment.
If name.ext is on your path then it will be edited, otherwise the editor will be started with name.ext in the current directory as the filename. If name.ext is not modifiable, it will be copied to HOME before editing.

Warning: You may need to clear name before the new definition is available. If you are editing a .cc file, you will need to execute mkoctfile name.cc before the definition will be available.

Qui una personalizzazione. Di default Octave ha impostato Emacs come editor. Ottima scelta ma sapete com’è 😉
Ovviamente si può (deve?) cambiare. Semplice, basta aprire il file ~/.octaverc (se non c’è crearlo) e aggiungere la riga: EDITOR ("/usr/bin/gedit") o quella del vostro editor preferito. Non so se serve tutto il percorso, nel dubbio… Ah! e si trova con which gedit 😳

o291

If edit is called with field and value variables, the value of the control field field will be set to value. If an output argument is requested and the first input argument is get then edit will return the value of the control field field. If the control field does not exist, edit will return a structure containing all fields and values. Thus, edit get all returns a complete control structure.

The following control fields are used:

  • home‘ This is the location of user local m-files. Be sure it is in your path. The default is ~/octave.
  • author’ This is the name to put after the “## Author:" field of new functions. By default it guesses from the gecos field of the password database.
  • email’ This is the e-mail address to list after the name in the author field. By default it guesses <$LOGNAME@$HOSTNAME>, and if $HOSTNAME is not defined it uses uname -n. You probably want to override this. Be sure to use the format user@host.
  • license ‘gpl’ GNU General Public License (default).
    bsd’ BSD-style license without advertising clause.
    pd’ Public domain.
    ‘”text”’ Your own default copyright and license.
    Unless you specify ‘pd’, edit will prepend the copyright statement with "Copyright (C) yyyy Function Author".
  • mode’ This value determines whether the editor should be started in async mode (editor is started in the background and Octave continues) or sync mode (Octave waits until the editor exits). Set it to "sync" to start the editor in sync mode. The default is "async" (see system).
  • editinplace’ Determines whether files should be edited in place, without regard to whether they are modifiable or not. The default is false.

Built-in Function: mfilename ()
Built-in Function: mfilename ("fullpath")
Built-in Function: mfilename ("fullpathext")

Return the name of the currently executing file.
When called from outside an m-file return the empty string.
Given the argument "fullpath", include the directory part of the file name, but not the extension.
Given the argument "fullpathext", include the directory part of the file name and the extension.

Built-in Function: val = ignore_function_time_stamp ()
Built-in Function: old_val = ignore_function_time_stamp (new_val)

Query or set the internal variable that controls whether Octave checks the time stamp on files each time it looks up functions defined in function files.
If the internal variable is set to "system", Octave will not automatically recompile function files in subdirectories of octave-home/lib/version if they have changed since they were last compiled, but will recompile other function files in the search path if they change.
If set to "all", Octave will not recompile any function files unless their definitions are removed with clear.
If set to "none", Octave will always check time stamps on files to determine whether functions defined in function files need to recompiled.

o292

Avanti, qui.

Manipolare la load path
When a function is called, Octave searches a list of directories for a file that contains the function declaration. This list of directories is known as the load path. By default the load path contains a list of directories distributed with Octave plus the current working directory. To see your current load path call the path function without any input or output arguments.
It is possible to add or remove directories to or from the load path using addpath and rmpath. As an example, the following code adds ‘~/Octave’ to the load path: addpath ("~/Octave").

After this the directory ‘~/Octave’ will be searched for functions.

Uh! da provare, il file lontano.m si trova in ~/tmp (sì, ho una tmp perso).

o293

Built-in Function: addpath (dir1, ...)
Built-in Function: addpath (dir1, ..., option)

Add named directories to the function search path.
If option is "-begin" or 0 (the default), prepend the directory name to the current path. If option is "-end" or 1, append the directory name to the current path. Directories added to the path must exist.
In addition to accepting individual directory arguments, lists of directory names separated by pathsep are also accepted. For example: addpath ("dir1:/dir2:~/dir3").

Built-in Function: genpath (dir)
Built-in Function: genpath (dir, skip, ...)

Save the unique portion of the current function search path that is not set during Octave’s initialization process to file.
If file is omitted, Octave looks in the current directory for a project-specific .octaverc file in which to save the path information. If no such file is present then the user’s configuration file ~/.octaverc is used.
If successful, savepath returns 0.
The savepath function makes it simple to customize a user’s configuration file to restore the working paths necessary for a particular instance of Octave. Assuming no filename is specified, Octave will automatically restore the saved directory paths from the appropriate .octaverc file when starting up. If a filename has been specified then the paths may be restored manually by calling source file.

Built-in Function: path (...)
Modify or display Octave’s load path.
If nargin and nargout are zero, display the elements of Octave’s load path in an easy to read format.
If nargin is zero and nargout is greater than zero, return the current load path.
If nargin is greater than zero, concatenate the arguments, separating them with pathsep. Set the internal search path to the result and return it.
No checks are made for duplicate elements.

Function File: val = pathdef ()
Return the default path for Octave.
The path information is extracted from one of four sources. The possible sources, in order of preference, are:

  • .octaverc
  • ~/.octaverc
  • <OCTAVE_HOME>/.../<version>/m/startup/octaverc
  • Octave’s path prior to changes by any octaverc file.

non spaventatevi, non tanto almeno:

o294

Built-in Function: val = pathsep ()
Built-in Function: old_val = pathsep (new_val)

Query or set the character used to separate directories in a path.
Ovvio che per noi è due-punti.

Built-in Function: rehash ()
Reinitialize Octave’s load path directory cache.

Built-in Function: file_in_loadpath (file)
Built-in Function: file_in_loadpath (file, "all")

Return the absolute name of file if it can be found in the list of directories specified by path.
If no file is found, return an empty character string.
If the first argument is a cell array of strings, search each directory of the loadpath for element of the cell array and return the first that matches.
If the second optional argument "all" is supplied, return a cell array containing the list of all files that have the same name in the path. If no files are found, return an empty cell array.

o295

Built-in Function: restoredefaultpath (...)
Restore Octave’s path to its initial state at startup.

Built-in Function: command_line_path (...)
Return the command line path variable.
Mysteryousassay 😳

Built-in Function: dir_in_loadpath (dir)
Built-in Function: dir_in_loadpath (dir, "all")

Return the full name of the path element matching dir.
The match is performed at the end of each path element. For example, if dir is "foo/bar", it matches the path element "/some/dir/foo/bar", but not "/some/dir/foo/bar/baz" "/some/dir/allfoo/bar".
If the optional second argument is supplied, return a cell array containing all name matches rather than just the first.

o296

Nota: pwd() è una funzione ma essendo senza argomenti le parentesi possono essere omesse (pessima abitudine, secondo me).

o297

Pausa, sarà lunga… 😀
:mrgreen:

Posta un commento o usa questo indirizzo per il trackback.

Trackback

Rispondi

Inserisci i tuoi dati qui sotto o clicca su un'icona per effettuare l'accesso:

Logo di WordPress.com

Stai commentando usando il tuo account WordPress.com. Chiudi sessione /  Modifica )

Google photo

Stai commentando usando il tuo account Google. Chiudi sessione /  Modifica )

Foto Twitter

Stai commentando usando il tuo account Twitter. Chiudi sessione /  Modifica )

Foto di Facebook

Stai commentando usando il tuo account Facebook. Chiudi sessione /  Modifica )

Connessione a %s...

Questo sito utilizza Akismet per ridurre lo spam. Scopri come vengono elaborati i dati derivati dai commenti.

%d blogger hanno fatto clic su Mi Piace per questo: