How to use a parameter as a global variable in a .m file?

To clearly describe what I mean, I'll just give an example:

function y = f(x,a)
global a
y = f1(x);

function y = f1(x)
global a
y = x + a;

Here I want the variable 'a' to be used as a global variable that can be called by the subfunction 'f1' to compute $ x + a $. (My goal is to reduce parameter conversion)

But this function does not work if I do not define a new variable 'b' to restore the value of 'a'.

The question is, how can I make the 'a' global variable directly without defining a new variable?

+3
source share
2 answers

I would not recommend using global variables, especially since you pass a to a function f.

, , , :

function y = f(x,a)
y = f1(x);
   function y = f1(x)
   y = x + a;
   end
end
+3

a=evalin('base','a');

a . .

0

All Articles