Can a function access variable which were not input variables?
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
lat = eval(strcat(varin, 'intlat')
lat = eval(strcat(varin, 'intlon')
DO NOT DO THIS.
Your approach is forcing 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:
"I'm trying to write a function to avoid repetition in my code, I and noticed that one task always comes in pairs"
Then you should put your data into one array and used loops and indexing or vectorized code. Indexing is simple, neat, easy to debug, and very efficient. Vectorized code is often simple and efficient. Unlike what you are doing.
Penny
el 12 de Oct. de 2018
Penny
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)
"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
Penny
el 12 de Oct. de 2018
"And I wasn't asking how to tell whether something was an input or not..."
That's good, because my answer does not "...tell whether something was an input or not", it lets you access variables in another workspace. I thought that is what you want to do.
"I also can't tell how your example would apply to my code."
Nested functions can access any variables in their "parent" function workspaces. You asked "...but they are in another workspace (the main code), so it can't do anything with them. Is there a solution to this?" and so I gave you a solution to that: just use nested functions and you can access any of the variables in the "parent" workspace.
"How do nested functions help?"
They let you access any variable in the "parent"/main function's workspace. Which answers your question.
Note that you should definitely avoid magically accessing variable names.
Penny
el 12 de Oct. de 2018
"Why should I definitely avoid "magically" accessing variable names?"
For the third time, here is the answer to that question:
It really depends on you: if you want to waste your time trying to debug inefficient code which has difficult-to-fix bugs, then this is perfect for you. If you would rather spend your time on something more productive, like actually getting results or sitting outside in the sun having a drink, then you should follow the advice of the MATLAB documentation and all experienced MATLAB users.
"I still don't think that helps as the whole point of using a function was so that it was not in the main code file and have to be repeated in several different places"
Probably the best option would be to put all of the parameters into one structure and pass that as an input argument. That makes accessing the data very neat, efficient, and easy to debug.
"I don't mind if it would normally be slow and buggy, speed isn't an issue and in this case it might actually help..."
Are you paid by the hour?
Penny
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):
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!