Advanced Octave A06 — Script context, parameters and identity
Advanced GNU Octave with GX-TXT · Module 01
A06 — Contesto, parametri e identità dello script
The same analysis can be reused with different parameters without changing the source code. In GX-TXT the parameter statement, the value chosen in the Runner and the execution context have distinct roles.
Objectives and prerequisites
After the introductory course and the first advanced lessons we can record and run a script. Here we declare a numerical parameter, we read it from the context and apply a scientific precondition. The test uses GX-TXT dev112-P011-bis and GNU Octave 9.4.0.
Declare and read a parameter
Import parametri_contesto.m, assign him an ID and save him in the Session. Open Run → Open Script Runner: Runner presents the frequency field with initial 5.0 value. Then start the Job.
% Advanced Octave course A06: typed parameters and execution context.
% SPDX-License-Identifier: GPL-3.0-or-later
gxtxt_parameter_declare('frequency', 'double', 'default', 5.0, ...
'min', 0.0, 'description', 'Frequency in Hz');
ctx = gxtxt_context();
frequency = ctx.parameters.frequency;
fprintf('Script: %s\n', ctx.script_name);
fprintf('Frequency: %.3f Hz\n', frequency);
if frequency <= 0
error('Frequency must be positive to compute a period.');
endif
fprintf('Period: %.3f s\n', 1 / frequency);
The statement describes name, type, default, lower limit and parameter description. The actual value comes from the context, here with ctx.parameters.frequency : the declaration call does not assign alone the variable Octave frequency.
The calculation uses the T = 1/f report. The declared limit admits zero, while the period requires a strictly positive frequency; for this the script performs an additional verification before the division. The description specifies hertz, while the period is presented in seconds: the unit has meaning in the model, it is not automatically added to an Octave number.
In the test the frequency chosen is 5 Hz and the period returned is 0,200 s. With 10 Hz the expected value is 0,100 s; repeat the script by changing the parameter and controlling the result. The context also includes information on the execution and identity of the script: it uses only the fields provided by the documentation of the installed version.
Reliable parameters
A type and limit statement facilitates interaction, but does not exhaust scientific verification. For example, a numerically acceptable value could be incompatible with the physical interval of a specific experiment. If the calculation depends on multiple parameters, it also checks the relationships between them and documents the values used in the result.
The save records the script and allows the framework to detect the allowed statements: change a copy of the external source file does not automatically update the script already recorded in the Session.
Exercise
- What is the expected period with frequency
2 Hz? - Why does the
frequency <= 0guard remain useful even if the stated minimum is zero? - Where is the value chosen in Runner read?
Worked solution
0,5 s.- The limit allows zero, but the formula of the period requires a positive frequency.
- From the
parameters.frequencyfield of the context returned bygxtxt_context().
The next module will deepen the diagram and reading of scientific datasets.
Other steps of this project
- Advanced Octave A75 — Dynamical systems in phase space
- Advanced Octave A81 — Geography of the waves and a 3D surface
- Advanced Octave A82 — Age, sex and demographic inequalities
- Advanced Octave A83 — All-cause mortality, certified COVID deaths and surveillance
- Advanced Octave A84 — A reproducible public account
- Advanced Octave A73 — Optimization landscapes and 3D trajectories