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 Comments

Why do you need such a thing?
"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.

Sign in to comment.

 Accepted Answer

Daniel Shub
Daniel Shub on 15 Feb 2012
Maybe another person chiming in will convince you this is a bad idea. That said, if I had to go down this road I would probably approach it something like I suggested here: http://www.mathworks.com/matlabcentral/answers/14512-cellfun-for-objects

3 Comments

Thanks Daniel,
This looks like a beginning of a solution. Would it work with "if" statements too?
I would guess that it could "work" with "if" statements. There is very little that cannot be accomplished with eval statements. That said, they make understanding and maintaining the code much more difficult and often eval makes writing the code more difficult too.
Daniel, I totally agree with you on this. The problem is that I am facing this tradeoff between writing a massive amount of files to disk and finding a way to have anonymous functions. I see a two-step implementation here. In the first, I could write the functions to disk and thoroughly test that I get the results that I want and in the second step, try the anonymous implementation. I will also have to see what difference it makes in terms of speed of execution.

Sign in to comment.

More Answers (2)

Jan
Jan on 15 Feb 2012
The M-file is fine and runs fast. Are you sure that you need an anonymous function?

4 Comments

Hi Jan,
Yes I do need anonymous functions, not just one. I would like to store the anonymous functions in memory inside an object. I can save the object, pass it on, reuse it, etc. If I have multiple files in addition, I would have to save the created m-files as well and the object will always depend on the files. I'd rather have either anonymous functions or an explicit method (for the object) that evaluates expressions. In a brief, anything that will help me avoid writing and saving a problem-specific m-file function to disk.
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.
Hi Jan, thanks for you answers. I understand your concerns about the possible bugs. In my case, I am 99.9999 percent sure there won't be any bug there.
On the other hand, besides the huge amount of m-files generated, another problem with creating m-files and creating handles to them is that if you change something in the input and you run the same program again, the handles may not be properly updated even after you first delete the existing files from the first run.
As an example, in one my runs I had a 3x7 matrix. Then I changed something that makes the matrix 3x9. When I ran the same program again, I kept having a 3x7 matrix.
Rather than having multiple functions created, if at least I could have one function that evaluates expressions (strings), I think I would prefer such a solution.
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.

Sign in to comment.

It is not possible to construct an anonymous function that specifies returning multiple outputs.

3 Comments

well it could return a cell array but >>why
Sean is right that one could always return a cell array.
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)

Sign in to comment.

Categories

Find more on Parallel Computing Toolbox in Help Center and File Exchange

Asked:

on 15 Feb 2012

Edited:

ebi
on 14 Oct 2013

Community Treasure Hunt

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

Start Hunting!