variable in local function in livescript

2 visualizaciones (últimos 30 días)
alpedhuez
alpedhuez el 2 de Mayo de 2020
Editada: Sai Sri Pathuri el 4 de Mayo de 2020
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?

Respuesta aceptada

Sai Sri Pathuri
Sai Sri Pathuri el 4 de Mayo de 2020
Editada: Sai Sri Pathuri el 4 de Mayo de 2020
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

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by