global variable and live script

9 次查看(过去 30 天)
JG
JG 2022-5-7
评论: JG 2022-5-10
Hallo everyone
I am using a live script where I declaire global variable and assign a value to the global variable with the control feature (from live script)
It seems that the assigned values of the global variables are only passed to the function (where I call the global variables) when I delcaire the global variable in the live script after using the control features.
When I delcaire the global at the beginning of the live script the called global variable in the functions are empty (0x0).
Does someone know how to use this directly?
Here an example:
this does not work
global userinp_ask
userinp_ask = false; %% this should be a drop down control from live script
my_function(); % here I call the global in the function
this does work
u_ask = false; %% this should be a drop down control from live script
global userinp_ask
userinp_ask = u_ask
my_function(); % here I call the global in the function
function myfunction()
global userinp_ask
% do something with userinp_ask
end

回答(1 个)

Harsha Kalavakolu
Harsha Kalavakolu 2022-5-10
As per my understanding you want to know why in the first case global variable "userinp_ask" is showing value as empty(0x0).
If the global variable does not exist the first time you issue the global statement, it is initialized to an empty 0x0 matrix. In the first case as global variable is not created yet it stores 0X0 matrix as its value.
In the second case you are assigning a value directly to global variable instead of declaring an empty global variable, that is why it is not storing 0X0 matrix.
I do not see any problem in the 1st method as well after you run the code, as the global variable data will be overwritten from empty matrix to false.
For more information on global variables go through the below documentation.
https://www.mathworks.com/help/matlab/ref/global.html
Hope it Helps.
  1 个评论
JG
JG 2022-5-10
Yes, I would agree with you.
The problem is the first method does not work, the global variable is updated in the live script but not in the function. Maybe its because I use live script controls?
thanks,
Jeremy

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by