Does a local function variable matter?
10 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Kevin Edlebeck
il 7 Feb 2018
Commentato: Walter Roberson
il 7 Feb 2018
When I am creating a function, does the variable I used to define it matter? When I call it in a script, I use it as the file name I saved it as. For example:
function E1=E_Voight(Vr,Em,Er)
E1=Er*Vr+Em*(1-Vr);
end
This is my function, I refer to it by its file save name (also E_Voight), but could I redefine this entire function using only x, y, z and change the file name to "Potato" and refer to it in a script as "Potato(1,2,3)" and the variables x,y,z used to define the function would not interfere with a variable that I had named x in my script?
0 Commenti
Risposta accettata
Walter Roberson
il 7 Feb 2018
Each function has its own workspace that does not interact with other workspaces unless you use evalin() or assignin(), or you use a global variable, or you use a shared variable (which requires nested functions.)
So yes, it is correct, if you were to define
function E1 = Potato(x, y, z)
then the x from the function definition would not interfere with any x in the script. The function cannot reach the variables in the script other than the ways I listed before. For example if you were to define
function E1=E_Voight(Vr,Em,Er)
E1=Er*Vr+Em*(1-Vr) + x;
then this would be treated as an undefined x (unless there happened to be a function named x): MATLAB would not look outside at the calling routine to try to find an x there.
2 Commenti
Walter Roberson
il 7 Feb 2018
MATLAB always knows the first function in .m function file by the name of the file (it will give you a warning if the name of the function differs from the name of the file.)
"So if I saved it as "Potato", that is what I would refer to it as in any of my other MATLAB scripts?"
Yep.
"And as long as my folder is in the MATLAB path, I will always be able to refer to those functions by their save name?"
Yep.
"Does that mean my save names should be complex so they do not interfer with actual variables in a script?"
In every case, if you assign to a variable in an "obvious" way within the function, then MATLAB will know to use that name as intending the variable instead of any function that might have the same name as the variable.
Historically in MATLAB, if you assigned to a variable in any way during execution of the function, then MATLAB would know to use that name as intending the variable instead of any function that might have the same name as the variable.
However, more recent versions of MATLAB are doing more and more static analysis that counts on there not being non-obvious ways of changing the variables; more and more now if you assign a variable in a way that is not obvious, then MATLAB might use the function by the same name instead of the variable.
The non-obvious ways of changing a variable are evalin(), assignin(), assigning a variable inside a script called by the function, and using load() without an output variable to create variables in the function.
For example, if you had
function p = Potato(x, y, z)
p = max([x, y, z]);
and if you had Chips.mat that stored a variable Potato = randi(20,5,5,5), and you had
function fry
load Chips
groovy = Potato(1, 2, 3);
disp(groovy)
then when MATLAB looks at this statically during the Just In Time compiling, it assumes that whatever happens to be in Chips.mat at the time of execution will not happen to include a variable name Potato, so the JIT will compile in a reference to the function Potato. If you put a breakpoint in, you can get confused easily because you would be able to see the variable Potato in the function workspace but the function Potato would be called anyhow.
The solution to this confusion is easy: Do not "poof" variables into existence. For example you could code
function fry
chips_data = load('Chips.mat');
Potato = chips_data.Potato;
groovy = Potato(1, 2, 3);
disp(groovy)
and then the change to Potato would be obvious and MATLAB will not get confused and accidentally think it should call the function.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Historical Contests in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!