[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Function handles for nonexisting functions
From: |
John W. Eaton |
Subject: |
Re: Function handles for nonexisting functions |
Date: |
Wed, 22 Apr 2009 22:24:21 -0400 |
On 22-Apr-2009, Judd Storrs wrote:
| On Wed, Apr 22, 2009 at 8:50 PM, John W. Eaton <address@hidden> wrote:
|
| > Create directories d1 and d2. Put the following fucntion in
| > d1/crash.m:
| >
| > function crash ()
| > 'd1/crash'
| >
| > and put the following function in d2/crash.m:
| >
| > function crash ()
| > 'd2/crash'
| >
| > Now start Matlab in the directory that contains d1 and d2, and run the
| > following commands:
| >
| > fh = @crash
| > fh ()
| > cd d1
| > fh ()
| > cd ../d2
| > fh ()
|
|
| >> fh = @crash
|
| fh =
|
| @crash
|
| >> fh ()
| ??? Undefined function or variable 'crash'.
|
| >> cd d1
| >> fh ()
|
| ans =
|
| d1/crash
|
| >> cd ../d2
| >> fh ()
|
| ans =
|
| d2/crash
So if the function handle is not defined initially, the definition
is not sticky? It changes depending on what function is currently
visible?
| clear all
| > fh = @crash
| > cd ..
| > fh ()
| > cd d1
| > fh ()
|
|
| >> clear all
| >> fh = @crash
|
| fh =
|
| @crash
|
| >> cd ..
| >> fh ()
|
| ans =
|
| d2/crash
|
| >> cd d1
| >> fh ()
|
| ans =
|
| d2/crash
In this case, there is a function available when the handle is created
and it appears to keep its original value, so that's different from
the first case, and more like what I would expect a function handle to
do.
| Now, while Matlab is still running, change the definition of
| > d2/crash.m to be
| >
| > function crash ()
| > 'foo'
| >
| > and then continue your Matlab session with the following commands:
| >
| > fh ()
| > cd ..
| > fh ()
| > cd d2
| > fh ()
|
|
| >> fh ()
|
| ans =
|
| d2/crash
|
| >> cd ..
| >> fh ()
|
| ans =
|
| d2/crash
|
| >> cd d2
| >> fh ()
|
| ans =
|
| foo
Hmm... So the new value is not picked up until you cd back to the
directory where it was first defined? Does that make sense? Seems
odd to me.
| > clear all
| > addpath (pwd)
| > cd ..
| > fh = @crash
| > fh ()
|
|
| >> clear all
| >> addpath(pwd)
| >> cd ..
| >> fh = @crash
|
| fh =
|
| @crash
|
| >> fh ()
|
| ans =
|
| foo
OK, the function is found through the path.
| Now, while Matlab is still running, change the definition of
| > d2/crash.m to be
| >
| > function crash ()
| > 'bar'
| >
| > and then continue your Matlab session with the following commands:
| >
| > fh ()
| > cd d1
| > fh ()
| > cd ../d2
| > fh ()
|
|
| >> fh ()
|
| ans =
|
| bar
|
| >> cd d1
| >> fh ()
|
| ans =
|
| bar
|
| >> cd ../d2
| >> fh ()
|
| ans =
|
| bar
So when the function is found through an absolute path element, the
new definition is found immediately?
Ugh. Is it just me, or does this seem like a lot of twisty special
cases?
jwe
- Re: Function handles for nonexisting functions, Judd Storrs, 2009/04/22
- Re: Function handles for nonexisting functions, John W. Eaton, 2009/04/22
- Re: Function handles for nonexisting functions, Judd Storrs, 2009/04/22
- Re: Function handles for nonexisting functions, Judd Storrs, 2009/04/22
- Fwd: Function handles for nonexisting functions, Judd Storrs, 2009/04/22
- Message not available
- Message not available
- Re: Function handles for nonexisting functions, Judd Storrs, 2009/04/22
- Re: Function handles for nonexisting functions, John W. Eaton, 2009/04/23
- Re: Function handles for nonexisting functions, Judd Storrs, 2009/04/23