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)
global aa bb 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 Comments
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/483172-how-to-create-global-variables-in-matlab-python-workspace#comment_751916
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/483172-how-to-create-global-variables-in-matlab-python-workspace#comment_751916
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/483172-how-to-create-global-variables-in-matlab-python-workspace#comment_752269
Direct link to this comment
https://fr.mathworks.com/matlabcentral/answers/483172-how-to-create-global-variables-in-matlab-python-workspace#comment_752269
Sign in to comment.