How to create global variables in MATLAB Python workspace

14 ビュー (過去 30 日間)
Mohd Shihabuddin Khan
Mohd Shihabuddin Khan 2019 年 10 月 2 日
コメント済み: Rik 2020 年 5 月 7 日
Consider the following case:
A main python file calls three different MATLAB functions -- func_initialize.m, func_1.m and func_2.m.
func_initialize take some arguments from the Python workspace, and creates a list of global variables: aa, bb and cc. These variables are then used in func_1 and func_2.
func_initialize is structured as follows:
function func_initialize(args)
% Do some calculations using args
global aa bb cc
% Set values of aa bb and cc
end
However, within the python script, when I call these functions from the workspace dictionary:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.workspace['aa']
it gives me the following error:
MatlabExecutionError:
File /usr/local/MATLAB/R2019a/toolbox/matlab/external/engines/engine_api/+matlab/+internal/+engine/getVariable.m, line 27, in getVariable
Undefined variable 'aa'.
What's wrong with the programming logic? Could there be a better way to share data between the three MATLAB files?
In my actual code, a Python object has some methods which call these three MATLAB functions. MATLAB engine is started by the class constructor. Then the class methods use this MATLAB engine to call various functions and access the variable names.
  2 件のコメント
Rik
Rik 2019 年 10 月 2 日
You need to declare global variables in every workspace. So if you set it in a function, it won't be in your main workspace.
Also, globals are a bad idea and you almost never need them.
Mohd Shihabuddin Khan
Mohd Shihabuddin Khan 2019 年 10 月 3 日
I've done that in the other scripts.
But it seems that these global variables aren't added to the workspace dictionary for access. Any idea about that?
I agree with your suggestion. I'll try sharing data between the scripts using *.mat files.

サインインしてコメントする。

回答 (1 件)

Shrinidhi KR
Shrinidhi KR 2020 年 5 月 7 日
You can use a getter function instead of accessing the variables as eng.workspace['aa'].
function aa = getvals()
global aa
end
In python script use the getter function to fetch the value of global variable:
import matlab.engine
eng = matlab.engine.start_matlab()
eng.func_initialize(1,2,3, nargout=0)
tf = eng.getvals()
print(tf)
A getter function like this can return the value of global variable set elsewhere in any other function, i.e in your case func_initialize.
The point to note is that when accessing the global variable inside any other functions they still need to be declared as "global same_variable_name" first (as shown above in 'getvals' function), which references to the same set of global variables and hence can access their values across.
The documentation also provides a similar setter and getter approach while sharing global varibales across functions in Matlab: https://www.mathworks.com/help/matlab/ref/global.html
  1 件のコメント
Rik
Rik 2020 年 5 月 7 日
I would strongly encourage you to use much longer names for your global variable to avoid name collisions.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeCall MATLAB from Python についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by