Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make matlab variable in workspace as global

In the workspace I make a matrix .

enter image description here

Now I can access the variable in script. Like doing Variable(2) will return 4.

But inside a function like

function y= getvariable(x)

y=Variable(x)
end

I get error

   y=getvariable(2)
    ??? Undefined function or method 'Variable' for input
    arguments of type 'double'.

    Error in ==> getvariable at 3
    y=Variable(x)

So how to make the Variable matrix global so that I can access it through any function?

like image 216
Moz Avatar asked Sep 05 '13 13:09

Moz


People also ask

How do I make a variable variable global in MATLAB?

A variable in MATLAB is set as global by writing a global command before the variable name(s).

How do you define a variable in MATLAB workspace?

To create a new workspace variable from an existing variable, in the Variables editor, select an element, data range, row, or column in an array, and then in the Variable tab, select New from Selection.

How do you load a variable in a workspace in MATLAB?

To load saved variables from a MAT-file into your workspace, double-click the MAT-file in the Current Folder browser. To load a subset of variables from a MAT-file on the Home tab, in the Variable section, click Import Data. Select the MAT-file you want to load and click Open.

How do you define a global variable?

Global variables, as the name implies, are variables that are accessible globally, or everywhere throughout the program. Once declared, they remain in memory throughout the runtime of the program. This means that they can be changed by any function at any point and may affect the program as a whole.


3 Answers

Although you could use globals

>> global Variable = rand(50,12);

...

function y = getvariable(x)

     % Always needed
     global Variable;

     % Here ya go
     y = Variable;

end

the MUCH better alternative is to use

function x = getvariable(x)
     % no body needed    
end

which you call as

>> y = getvariable(Variable);

(Of course, for this contrived example, this would just be equal to

>> y = Variable;

)

Although there are some legitimate use cases for global variables, in general they tend to spaghettify your code and make it far more bug-prone and much harder to debug. Have a read on the subject.

like image 108
Rody Oldenhuis Avatar answered Nov 15 '22 10:11

Rody Oldenhuis


As @rody suggested, pass the matrix and the x inside the function

I am just giving an example to make things clear.

Like you want to access the 10th element of Variable matrix, so make the function as

function y= getvariable(matrixname,no)
y=matrixname(no)
end

If you want to access 3rd element of Variable, so you type

y=getvariable(Variable,3)

you will get 3rd element

like image 45
S P Avatar answered Nov 15 '22 12:11

S P


  1. call global Variable before you define it in your workspace
  2. call global Variable before you use it in your function

However I suggest you think of other ways to pass variables to your function, as globals might cause difficulties during debugging.

like image 39
Nick Avatar answered Nov 15 '22 10:11

Nick