How to call a function from outside itself ?
显示 更早的评论
Altought it is basic i cannot understand how it works in Matlab 2017..
I have 2 simple functions
function y1=square(x1)
y1=x1^2;
end
function y2=cube(x2)
y2=x2^3;
end
i can call square function but i cannot call cube...
>> square(5)
ans =
25
>> cube(5)
Undefined function or variable 'cube'.
why does this happen?
I cannot also use global, persistent to help with this...
采纳的回答
"why does this happen?"
Because local functions are only callable within the same file. This is explained in the documentation: "In a function file, the first function in the file is called the main function. This function is visible to functions in other files, or you can call it from the command line. Additional functions within the file are called local functions, and they can occur in any order after the main function. Local functions are only visible to other functions in the same file."
If you want to call both functions outside of that file then by far the easiest way to achieve that is to write each function in its own file. If you really really need to call a local function outside of its own file then create a handle to it and pass that handle as an output argument (I have only rarely seen use cases where this is required).
7 个评论
So instead of creating a second flile with the cube function and call it ,how could i call this local function (cube) from the file with main and local functions not from command line but from file?
"how could i call this local function (cube) from the file with main and local functions"
Just like you call any other function:
function y = test(x)
y = cube(x);
end
function y2=cube(x2)
y2 = x2^3;
end
Ok Ok !!
Now i understand your point.!!
Leat me ask something more please
Yesterday i found also this:
and have some questions about the same issue;
1)in the Best Practice: Passing Arguments
why functions dont close with ""END""??
2) in global and persistent altough i copy the code it doesnt seem to recognize in matlab17
thnak you very much for support!!
"why functions dont close with ""END""??"
The end keyword is not always required (but I strongly recommend that you always use it). From the function documentation:
"For readability, use the end keyword to indicate the end of each function in a file. The end keyword is required when:"
- "Any function in the file contains a nested function."
- "The function is a local function within a function file, and any local function in the file uses the end keyword."
- "The function is a local function within a script file."
Note that you can easily search for this information yourself (try using your favorite internet search engine).
"in global and persistent altough i copy the code it doesnt seem to recognize in matlab17"
You do not explain exactly what code you tried or what you expect to get. How did you check that "it doesnt seem to recognize" ?
Very helpfull infos!!And yes i use google but i need a more ''professional'' opinion!!
Now the global worked!!!
I think Matlalb is a vey stubborn tool and doesnt forgive easily!!!!hahaha
thank you very much!!!!
"And yes i use google but i need a more ''professional'' opinion!!"
The "professional" opinion is that you should avoid using global variables:
The MATLAB documentation states "Use global variables sparingly, if at all":
thank you very much!!
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Get Started with MATLAB 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
