No BSD License  

Highlights from
Closures in the MATLAB Language

3.0

3.0 | 1 rating Rate this file 3 Downloads (last 30 days) File Size: 729 Bytes File ID: #18223

Closures in the MATLAB Language

by Jim Hunziker

 

04 Jan 2008 (Updated 04 Jan 2008)

Demonstration of Closures in the MATLAB Language

| Watch this File

File Information
Description

Sometimes in MATLAB, a programmer might want to write functions that maintain state without exposing that state to the global workspace.

A closure is a concept in computer science that describes the execution of a function that has privately captured variables from an enclosing scope. Through nested functions and function handles, a user can create closures in MATLAB.

By privately capturing variables, a closure can help the programmer avoid maintaining global state variables and write more modular code.

This simple example shows how to write a function that takes an argument n and returns another function. This returned function can be called multiple times, returning n plus the number of times the function has been called previously.

Here is another useful application of closures. Suppose you have to do an operation that needs to work on every item in a sequence. There are many many items, and you can't afford the space to calculate them all at once, store them, and do the operation on them in a loop.

But you have plenty of time and CPU power. You can make a function like makeIncrementer (in the attached example) that remembers where it was in the sequence and generate each item on demand.

Learning to use closures can make your MATLAB routines smaller and your code more modular.

MATLAB release MATLAB 7.0.4 (R14SP2)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
04 Jan 2008 Scott Miller

It is an interesting approach. It seems to me to be more natural to use the 'persistent' built-in, though. Under what circumstances would this technique be preferred to 'persistent'?

Scott

05 Jan 2008 Jim Hunziker

The "persistent" built-in would be able to achieve the same functionality as the example, but closures are a more general and powerful abstraction.

Closures are available in languages like Python, Ruby, LISP, Scheme, and Javascript. For the most part, they are not available (or aren't fully implemented) in languages like C, C++, and Java.

To learn more about other things you can do with closures, I encourage you to visit the following sites (that explain things more thoroughly than I can):

http://en.wikipedia.org/wiki/Closure_(computer_science)
http://c2.com/cgi/wiki?WhatIsClosure
http://sreeram.cc/?p=8
http://programming.reddit.com/info/1v4b9/comments

05 Jan 2008 Jim Hunziker

Here's one more link where I found someone else who discovered that closures work correctly in MATLAB:

http://blogs.mathworks.com/loren/2007/08/09/a-way-to-create-reusable-tools/#comment-16367

06 Jan 2008 Dimitri Shvorob

Three observations if I may:

1. If a closure is a 'function that has privately captured variables from an enclosing scope', then a nested function fits the bill, and there is no need to bring in persistence, which is the focus of your example.

2. Responding to a question about the added benefits of this approach compared to the use of 'persistent' by saying that 'closures are a more general and powerful abstraction' is not very helpful.

3. It's persistence, and the backdoor way of creating *objects* that apparently is available in Matlab, that are the revelation/shocker here. Thank you for providing the link to Loren Shure's blog entry; I would recommend everyone to follow the link, and go directly to the illuminating comments (#3 and #6) by Michael Corvin. Michael's sample code shows everything that this submission does, and more.

06 Jan 2008 Dimitri Shvorob

Please allow me to retract the last sentence of my previous comment: the code here has a twist that's not in Michael Corvin's examples. In any case, I would definitely recommend examining this submission.

09 Jan 2008 Mikhail Poda

BEWARE!!! using nested functions is a nice approach to emulate oop, but there is a number of problems associated with their use in Matlab:

1. Nested functions are not automatically garbage collected by Matlab, you should do it manually otherwise you Matlab session would be getting slower and slower … We always define a method DISPOSE for each object, and call it when an object needs to be destroyed, it looks like
function public_dispose()
vars = who; clear(vars{:});
end

2. You should not have cyclic dependences between several objects, e.g. define a tree where node objects have fields this.parent and this.children with parent and children fields containing (pointing to) other node objects. If you have such cyclic dependencies in your code and call any method on the node object your performance will go to zero. It seems that Matlab tries to update all fields and when fields are objects then it updates these objects altogether and so originates a recursion.

Another point is that it is a good practice to have only one variable (of type struct) in the nested scope, for example call it *this* (like in java) and store all object data in the fields of *this*

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
whitepaper Jim Hunziker 22 Oct 2008 09:42:03
article Jim Hunziker 22 Oct 2008 09:42:03
paper Jim Hunziker 22 Oct 2008 09:42:03
closures language Jim Hunziker 22 Oct 2008 09:42:03
demonstration of closures Jim Hunziker 22 Oct 2008 09:42:03
white paper Jim Hunziker 22 Oct 2008 09:42:03

Contact us at files@mathworks.com