Can a function access variable which were not input variables?
28 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I'm trying to write a function to avoid repetition in my code, I and noticed that one task always comes in pairs. So I tried to write a function to do two at a time like this:
function [intlat, intlon] = diffhy(varin)
lat = eval(strcat(varin, 'intlat')
lat = eval(strcat(varin, 'intlon')
intlat = diff(lat,1))/-2;
intlon = diff(lat,1))/-2;
end
The problem is that it identifies variables to perform an operation, but they are in another workspace (the main code), so it can't do anything with them. Is there a solution to this? I've heard of global variable but also that they really aren't recommended.
4 comentarios
Stephen23
el 12 de Oct. de 2018
Editada: Stephen23
el 12 de Oct. de 2018
The simplest solution would be for you to put those variables into a structure, and then simply pass that structure as an input argument: this would be simple, efficient, easy to debug... which is why it is the recommended way to pass many parameters to a function.
This is NOT recommended:
lat = eval(strcat(varin, 'intlat')
lat = eval(strcat(varin, 'intlon')
DO NOT DO THIS.
Your approach is forcing you down a very bad path. Using eval and/or magically accessing variables in other workspace is one way that beginners force themselves into writing slow, complex, buggy code that is hard to debug. Read this to know why:
"Is there a solution to this? I've heard of global variable but also that they really aren't recommended."
Global variables should be avoided. Magically accessing variable names should be avoided. The best way to pass data from one function to another is to simply pass it as input/output arguments. This is exactly what the MATLAB documentation recommends:
Respuestas (1)
Stephen23
el 12 de Oct. de 2018
Editada: Stephen23
el 12 de Oct. de 2018
"Can a function access variable which were not input variables?"
function mainfun()
a = 'not an input to NESTFUN';
nestfun()
function nestfun()
disp(a)
end
end
And tested:
>> mainfun()
not an input to NESTFUN
Or just put the data into one structure and pass that as an input argument: simple, efficient, easy to debug.
6 comentarios
Stephen23
el 12 de Oct. de 2018
Editada: Stephen23
el 12 de Oct. de 2018
"What I want to know is whether it's possible not whether you like it."
It is possible, but not really with eval: the link I gave you lists the functions that you could use to magically access variables in a calling function, so I am sure that you didn't miss them.
"I'm not sure why you've got such a bee in your bonnet about it."
Lol. I guess this means the authors of MATLAB must also have bees in their bonnets, because they devoted a whole page of the help specifically advising users to avoid doing what you are trying to do (note that while this page uses eval for its examples, it applies to all methods of string evaluation):
Ver también
Categorías
Más información sobre Special Functions en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!