Is there a way to declare global variables in MATLAB?
Please don't respond with:
global x y z;
Because I can also read the help files.
I've declared a global variable, x, and then done something like this:
function[x] = test()
global x;
test1();
end
Where the function test1() is defined as:
function test1()
x = 5;
end
When I run test(), my output is x = []. Is there a way I can make it output the x=5, or whatever I define x to be in a separate function? In C, this would be an external variable, and I thought making it a global variable should accomplish just that.
You need to declare x as a global variable in every scope (i.e. function/workspace) that you want it to be shared across. So, you need to write test1 as:
function test1()
global x;
x = 5;
end
Referring to your comment towards gnovice using a global variable can be an approach to solve your issue, but it's not a commonly used.
First of all make sure that your .m files are functions and not scripts. Scripts share a common workspace, making it easy to unwillingly overwrite your variables. In contrast, functions have their own scope.
Use xUnit in order to generate repeatable unit test for your functions. By testing each function involved in your program you will track down the error source. Having your unit test in place, further code modifications, can be easily verified.
A possible way to get around the global mess is to assign the variable as appdata. You can use the functions setappdata and getappdata to assign and retrieve appdata from a MATLAB window. As long as a MATLAB session is active there exists a window denoted by 0.
>> setappdata(0,'x',10) % 0 indicates the root MATLAB window
Now the variable x is not visible to any script or function but can be accessed wherever needed by using getappdata.
function test
globalX = getappdata(0,'x');
disp(globalX);
end
x =
10
The good news is that you can assign any valid MATLAB object to appdata, just be cautious with the names, using unique names for appdata fields like ModelOptimizerOptions instead of a generic x,y would help. This works on compiled executables and code deployed on the MATLAB production server as well.
Related
I'm using Octave (or Matlab... I have both available), and trying to make a function that will setup my important variables used throughout the rest of my investigation.
However by default, variables declared in a function have a limited scope of that function only.
Is there a way to do this?
function defineBasicTerms()
global G = 9.807;
global F = [exp(1) 0; 0 1/exp(1)];
endfunction
such that after this function is called, variables G and F exist in the set of globally named variables?
Global variables require to be declared in each scope in which they are used. For example, if you have a function M-file defineBasicTerms.m containing:
function defineBasicTerms
global G = 9.807;
end
then in the base workspace you can write:
defineBasicTerms
G % produces error: variable doesn't exist
global G
G % gives 9.807
Next, in a function that will use the constant G:
function out = computeSomethingImportant(m)
global G
out = G * m;
end
That is, every time we want to use G we need to do global G to be access the global variable G.
Note that this is a very dangerous situation. Two things can go wrong:
Some function assigns to the global variable G, changing its value for the rest of the current session. It is really hard to know that this has happened, but computeSomethingImportant will return the wrong values from this point on, until we again call defineBasicTerms.
We call computeSomethingImportant before calling defineBasicTerms during the session.
The established method in MATLAB (and by extension in Octave) to define a constant is through a function. Functions can be written to always return the same value (MATLAB has no other way to declare a variable to be constant), and functions are automatically available in all workspaces and all contexts (as long as the function is on the path of course).
In your example, we'd write an M-file function G.m containing:
function value = G
value = 9.807;
end
Now, in the base workspace:
G % gives 9.807
The function that uses the constant G now looks simply like this:
function out = computeSomethingImportant(m)
out = G * m;
end
Note that constants such as pi are defined in this way in MATLAB and Octave.
You would have to write one function M-file for each of the constants you want to declare. There is an alternative method that involves a class with static values, so that all constants can be defined in a single file. The syntax then however becomes different, one would need to use constants.G, or something like that, to access the content value.
I have a MATLAB function to solve a Inertia Tensor , and I have a nested function in my program . All the variables in it are symbolics but it told me
“Error using assignin: Attempt to add ”x“ to a static workspace”
and I don't understand why this happens . Here is my test.m code:
function test
syms x y z
f=x
f1=f+1
f2=f1^2
function r=test2
r=f2^3;
end
f3=test2
end
After searching this web-forum I have found some answers . But at the same time I just don't understand it
Andrew Janke explianed it like this : While syms A may look like a static variable declaration, it isn't. It's just a regular function call. It's using Matlab's "command" invocation style to look like syntax, but it's really equivalent to syms('a', 'b', 'c').
on this page : Matlab: "Error using assignin: Attempt to add "c" to a static workspace"
what does static variable mean ?
I also search the HELP doc and it said :In functions and scripts, do not use syms to create symbolic variables with the same names as MATLAB® functions. For these names MATLAB does not create symbolic variables, but keeps the names assigned to the functions.
I only know syms x to create a symbolic variable in the workspace but why does the documentation say MATLAB does not create ?
'Static' means fixed, 'workspace' is what Matlab calls the places where all of its variables are stored. For non-nested functions the workspace starts off as empty when Matlab is at the beginning of the function; as Matlab continues through function's lines of code it continuously add more variables to the workspace.
For functions with a nested function, Matlab first parses the function to see what variable will be created (it specifically looks for x = type lines), then it creates all of these variables (with value as 'unassigned'), and then only does it start to run through the code; but while running through the code, it can never create a new variable.
This is why the code
function TestNestedFunction
syms x;
function Nested()
end
end
generates an error, there is no x = to tell it to pre-create the unassigned variable x at the start of the code. It fails at syms x;, as that line tries to create a new variable x, which fails as it may not.
This is also why the following code runs
function TestNestedFunction
syms x;
x = x;
function Nested()
end
end
it sees the x = and then pre-creates x. (This is why your example of adding [x, y, z] = deal([]); also works).
You can test this with a break point at the beginning of simple non-nested function and a simple nested function. Just run it step by step.
This code works:
function test
x=sym('x')
y=sym('y')
z=sym('z')
f=x
f1=f+1
f2=f1^2
function r=test2
r=f2^3;
end
f3=test2
end
I think the pages you found are quite clear.
You need to declare the variables one by one and use:
x = sym('x')
Otherwise syms will try to assign the values into a workspace where this is not allowed.
In the workspace I make a matrix .
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?
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.
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
call global Variable before you define it in your workspace
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.
For example in testinit.m I have the following
function [x, y, m] = testinit
x=4
y=3
m=2
When I run testinit in the console it correctly displays the value. However when I type x it says
error: 'x' undefined...
Just to add to the above answer, the reason you're getting this is because variables in a MatLab function are local variables, they are not passed to the workspace unless you use one of the functions in the above answer. You can read more about global and local variables here.
P.S If you wrote an m-file that is not a function, then the variables are global.
There's the assignin function (evalin is related). And also global.
The other answers are all possible solutions, but potentially more complicated than what you may be looking for. I think the first part of yuk's answer addresses the real problem you are having, but I think it deserves a more detailed explanation...
If you have a function that has output arguments, you need to actually capture those arguments in variables when you call the function. For example, if you typed this in the Command Window:
[x, y, m] = testinit;
Then you would have your three output values present for you to use. What you were probably doing was typing this:
testinit;
This would display the values (because you didn't end each line in the function with a semicolon to suppress displaying them), but it would not store them in variables in the Command Window for you to use later.
This is a result of how variables are stored in MATLAB, as described by the documentation on variable scope:
MATLAB stores variables in a part of memory called a workspace. The base workspace holds variables created during your interactive MATLAB session and also any variables created by running scripts. Variables created at the MATLAB command prompt can also be used by scripts without having to declare them as global.
Functions do not use the base workspace. Every function has its own function workspace. Each function workspace is kept separate from the base workspace and all other workspaces to protect the integrity of the data used by that function. Even subfunctions that are defined in the same file have a separate function workspace.
So, if you want to share variables between functions, the simplest way is to pass them back and forth via their input and output argument lists.
It should also be noted that the names you give variables in the output argument list of the function don't have to match the names of the variables you place those output values in. For example, given this function:
function [a, b, c] = testinit
a = 4;
b = 3;
c = 2;
You can make this call in the Command Window:
[x, y, m] = testinit;
And you will get x = 4, y = 3, and m = 2.
If you run [x, y, m] = testinit in the console you should get the variables. The output variables can have any allowed name, not necessary x, y and m.
In addition you should put ; after each variable assignment in the function to avoid their output to the console. You can control the console output while calling the function.
If you want simply initialize new variables just by typing testinit, use assignin as in #BenVoigt's answer.
assignin('base','x',4)
However, this is dangerous, since some variable may already exist in the calling environment and will be overwritten. You can avoid it adding EXIST tests (inside EVALIN):
function testinit
if ~evalin('base','exist(''x'',''var'')')
assignin('base','x',4)
end
if ~evalin('base','exist(''y'',''var'')')
assignin('base','y',3)
end
if ~evalin('base','exist(''m'',''var'')')
assignin('base','m',2)
end
You can also use 'caller' instead of 'base' if you plan to call the function from another function.
The variables are local to the function so you cannot access them from the command line. As #BenVoigt said, you can use asignin but it's very dirty, and I don't think that it's what you really want to do.
I advise you do go in debug mode
Add a break point or a keyboard to your function like that:
function [x, y, m] = testinit
x=4
y=3
m=2
keyboard
After execute your function, and the command line will remain in the environment of the function.
The usual >> kill be replaced by a K>>. At that point you can access all your local variables.
To quit the debug mode type dbquit, or press shift+F5
Is there a way to declare global variables in MATLAB?
Please don't respond with:
global x y z;
Because I can also read the help files.
I've declared a global variable, x, and then done something like this:
function[x] = test()
global x;
test1();
end
Where the function test1() is defined as:
function test1()
x = 5;
end
When I run test(), my output is x = []. Is there a way I can make it output the x=5, or whatever I define x to be in a separate function? In C, this would be an external variable, and I thought making it a global variable should accomplish just that.
You need to declare x as a global variable in every scope (i.e. function/workspace) that you want it to be shared across. So, you need to write test1 as:
function test1()
global x;
x = 5;
end
Referring to your comment towards gnovice using a global variable can be an approach to solve your issue, but it's not a commonly used.
First of all make sure that your .m files are functions and not scripts. Scripts share a common workspace, making it easy to unwillingly overwrite your variables. In contrast, functions have their own scope.
Use xUnit in order to generate repeatable unit test for your functions. By testing each function involved in your program you will track down the error source. Having your unit test in place, further code modifications, can be easily verified.
A possible way to get around the global mess is to assign the variable as appdata. You can use the functions setappdata and getappdata to assign and retrieve appdata from a MATLAB window. As long as a MATLAB session is active there exists a window denoted by 0.
>> setappdata(0,'x',10) % 0 indicates the root MATLAB window
Now the variable x is not visible to any script or function but can be accessed wherever needed by using getappdata.
function test
globalX = getappdata(0,'x');
disp(globalX);
end
x =
10
The good news is that you can assign any valid MATLAB object to appdata, just be cautious with the names, using unique names for appdata fields like ModelOptimizerOptions instead of a generic x,y would help. This works on compiled executables and code deployed on the MATLAB production server as well.