global variable and live script

조회 수: 6 (최근 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에서 Variables에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by