Can a function access variable which were not input variables?

51 次查看(过去 30 天)
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 个评论
Penny
Penny 2018-10-12
Also how could I use loops or indexing when the repetitions are not numbered? They are called things like "temp", "sal", "u" and "v", I'm not sure how to loop over that.
Stephen23
Stephen23 2018-10-12
编辑:Stephen23 2018-10-12
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:

请先登录,再进行评论。

回答(1 个)

Stephen23
Stephen23 2018-10-12
编辑:Stephen23 2018-10-12
"Can a function access variable which were not input variables?"
Yes: this is easy using nested functions:
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 个评论
Penny
Penny 2018-10-12
I still can't see anything which says whether it's even possible to use "eval" inside a non-nested function. If it's not, whether it's a style you like is completely irrelevant and I'm not sure why you've got such a bee in your bonnet about it. What I want to know is whether it's possible not whether you like it.
Stephen23
Stephen23 2018-10-12
编辑:Stephen23 2018-10-12
"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):

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Scope Variables and Generate Names 的更多信息

标签

产品


版本

R2016a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by