Code covered by the BSD License  

Highlights from
func2func.m v1.0 (Jul 2009)

Be the first to rate this file! 0 Downloads (last 30 days) File Size: 4.35 KB File ID: #24876

func2func.m v1.0 (Jul 2009)

by Carlos Adrian Vargas Aguilera

 

29 Jul 2009

Updates anonymous function workspace. Workaround of STR2FUNC(FUNC2STR(f)) problem.

| Watch this File

File Information
Description

Since MATLAB v7 we can use the new anonymous functionality to create small functions like:

>> A = 7;
>> F = @(x) A*x;
to get a simple multiplication by 7:
>> F(2)
ans =
14

But, if you change the A value, it is not updated on the F workspace:
>> A = 8;
>> F(2)
ans =
14

This is sometimes undesirable, and we guess that by using
>> G = str2func(func2str(F));
we fix this problem but then we get a warning
>> G(2)
??? Undefined function or method '@(x)A*x' for input
arguments of type 'double'.

So, one is forced to use the EVAL function. This FUNC2FUNC function uses EVAL and EVALIN to solve this problem, and the user can even specify the workspace to be used: 'base' or 'caller' or directly the function F workspace like:
>> clear A
>> F = func2func(F,'A',8);
>> F(2)
ans =
16

Besides it can be used to transform any array, cell, structure of functions names or handles into "updated" function handles.

Enjoy it!

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
str2func Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47
func2str Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47
function handle Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47
functions Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47
anonymous Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47
eval Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47
evalin Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47
workspace Carlos Adrian Vargas Aguilera 30 Jul 2009 10:50:47

Contact us at files@mathworks.com