You have to declare a variable as global in each function you want to use it, of course. Otherwise it would be easy to let Matlab crash massively by declaring any frequently used variables like "x" or "K" globally as global.
It is explained in the documentation clearly, how globals are used. Therefore a
would help already.
Note that global variables are known to cause troubles and impede the debugging. Clean code does not contain any global variables for this reason. Better store the data in a figure and share its data using e.g. the ApplicationData.
Best Answer