How to avoid writing a script function ?
显示 更早的评论
Dear all, I have the following problem: I would like to avoid writing a function as an m-file. Instead (and ideally) I would like to have it created as an anonymous function handle. The m-file equivalent of the function could look as complicated as the following:
function [a,b,c]=myfunc(d,e,f,g)
a=myfunc2(d,e,f);
if a>0
b=[];
c=[];
return
end
[b,c]=myfunc3(a,d,e,f,g);
Originally I thought a shortcut would be to simply create the m-file, create a handle to it and then delete the m-file. But I quiclkly realized it would not work. Is there any way to do to it, even using objects in some way or another?
Thanks, Patrick
2 个评论
Friedrich
2012-2-15
Why do you need such a thing?
per isakson
2012-2-15
"Warum soll man es einfach machen, wenn man es so schön komplizieren kann". Seriously, I need a better description to understand what you may gain by such a construct.
采纳的回答
更多回答(2 个)
Jan
2012-2-15
0 个投票
The M-file is fine and runs fast. Are you sure that you need an anonymous function?
4 个评论
Patrick Mboma
2012-2-15
Jan
2012-2-15
Hiding calculations in anonymous functions in an object impedes the debugging and maintenance of the code. I recommend to avoid highly sophisticated methods of meta-programming when using Matlab. Of course it can work. But the complexity of the resulting code is a magnet for bugs. In large programs, which are created using proper code- and project management as well as automated tests, each line of code is re-written 2 to 3 times and the result has about 1 bug per 1000 lines (a factor of 2 or 10 does not matter here).
In consequence D.E. Knuth's suggestion "keep it simple stupid" is useful for small and large programs. If you have a medium size project, think twice, if it is not growing in the future because you do not need this, or because the exploding complexity does not allow further modifications.
Or in other words: Using strange techniques for strange tasks is usually not efficient.
Patrick Mboma
2012-2-15
Jan
2012-2-16
I still do not see the drawback of the M-files. Of course you have to remove a loaded file from the memory using CLEAR FILENAME, after the file has been modified.
But I agree that even creating a large number of M-files dynamically means a very high complexity and slow execution speed. Loading and parsing an M-file the first time needs a lot of resources.
Anyhow, I'm sure you will remember our warnings when they will get important in the future. And if they are not getting important, this is even better! Good luck.
Walter Roberson
2012-2-15
0 个投票
It is not possible to construct an anonymous function that specifies returning multiple outputs.
3 个评论
Sean de Wolski
2012-2-15
well it could return a cell array but >>why
Patrick Mboma
2012-2-15
Teja Muppirala
2012-2-16
You can return multiple outputs from an anonymous function by using DEAL, for example:
F = @(x) deal( x, 2*x, 3*x )
[a,b,c] = F(10)
类别
在 帮助中心 和 File Exchange 中查找有关 Parallel Computing Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!