global variable and live script

10 views (last 30 days)
JG
JG on 7 May 2022
Commented: JG on 10 May 2022
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

Answers (1)

Harsha Kalavakolu
Harsha Kalavakolu on 10 May 2022
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 Comment
JG
JG on 10 May 2022
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

Sign in to comment.

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!