A.1.7 Доступ к глобальным переменным в Oct-файлах
Глобальные переменные позволяют обращаться к переменным в глобальной области видимости. Доступ к глобальным переменным из Oct-файлов можно получить с помощью вспомогательных функций global_varval и global_assign из таблицы символов текущего интерпретатора. Первым аргументом обеих функций является строка с именем переменной, значение которой нужно получить или присвоить. Вторым аргументом функции global_assign является присваиваемое значение. Пример использования этих двух функций:
#include <octave/oct.h>
#include <octave/interpreter.h>
DEFMETHOD_DLD (globaldemo, interp, args, , "Global Demo")
{
if (args.length () != 1)
print_usage ();
octave_value retval;
std::string s = args(0).string_value ();
octave::symbol_table& symtab = interp.get_symbol_table ();
octave_value tmp = symtab.global_varval (s);
if (tmp.is_defined ())
retval = tmp;
else
retval = "Global variable not found";
symtab.global_assign ("a", 42.0);
return retval;
} Пример использования:
global a b
b = 10;
globaldemo ("b")
⇒ 10
globaldemo ("c")
⇒ "Global variable not found"
num2str (a)
⇒ 42
© 1996–2026 The Octave Project Developers
Permission is granted to make and distribute verbatim copies of this manual provided the copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the conditions for verbatim copying, provided that the entire resulting derived work is distributed under the terms of a permission notice identical to this one.Permission is granted to copy and distribute translations of this manual into another language, under the above conditions for modified versions.
https://docs.octave.org/v9.4.0/Accessing-Global-Variables-in-Oct_002dFiles.html