variable in local function in livescript

조회 수: 9 (최근 30일)
alpedhuez
alpedhuez 2020년 5월 2일
편집: Sai Sri Pathuri 2020년 5월 4일
In livescript,
I define
a=1;
Then define a local function
function y = multi(x)
y = a*x;
end
This will give an error message. How can I define a as a global variable?

채택된 답변

Sai Sri Pathuri
Sai Sri Pathuri 2020년 5월 4일
편집: Sai Sri Pathuri 2020년 5월 4일
The variable a is defined in base workspace and hence, the error is because of the unavailability of variable a in function workspace. To share the data from base workspace with function workspace, you may make the variable a global.
global a
a = 1;
You also need to declare a as global in the function
function y = multi(x)
global a
y = a*x;
end
You may refer the following documentation for different practices of sharing data between workspaces
You may refer the following docmentation for differences between base and function workspace

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Scope Variables and Generate Names에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by