Dawn
Dawn

Reputation: 3628

Declare variables for the entire scope in Matlab

I'm been working on a GUI in which many functions use the object. Currently, this object is declared as global variable in each of the subfunction in the gui. This object is also used by timers so direct reference fun1(myobject) not always working right.

function fun1
global myobject
...
function fun2
global myobject
...

I wanted to know if there's a smarter way of declaring this variable for the entire scope of the GUI m-file. I've tried declaring it outside the functions but it didn't work.

myobject = 1
function fun1
...
function fun2
...

Upvotes: 1

Views: 655

Answers (1)

Itamar Katz
Itamar Katz

Reputation: 9645

Use nested sub-functions:

function myGui
a = 5;
f1();
f2();

    function f1 ()
        disp (a)
    end

    function f2 ()
        disp (a)
    end
end

Upvotes: 5

Related Questions