MATLAB: Global variables in functions

functionsglobal variable

I have been trying to use global variables so I can make changes to the said variable in different functions but I can't seem to do it. It seems like the second function does not work. Please help.
function one=new1()
clc
clear global
global m
m=0;
disp('soup')
function hamburger=eat()
m=m+1
disp('sandwich')

Best Answer

Get rid of the "clear global" function in new1(), which blew away your existing globals, and add "global m" to eat():
function one=new1()
global m
clc
m=0;
disp('soup')
function hamburger=eat()
global m
m=m+1
disp('sandwich')
And the variables are only global for the functions that actually have the "global m" line in them, not to other functions that don't have that line.