How can I get Matlab to use the variable value instead of name

Chachmu

In my code, I have a line that looks like this:

f=@(test) bf{i}(5);

where bf is a cell array with functions from str2func() stored in it, i is a variable storing an integer, and the 5 is the argument to pass to the function. How can I get matlab to evaluate the line using the current value of i? Right now when I display f it outputs:

@(test)bf{i}(5)

Lets say i=1, I want it to output:

@(test)bf{1}(5)

Although technically the bf{1} should also be replaced with whatever function is stored in bf{1}. How can I force matlab to evaluate the variables in this statement?

Daniel

When you create a function handle, the workspace variables are copied and the expression is evaluated when you call the function handle (Typically not a problem in memory consumption, matlab stores only changes).

Now the problem is, to tell Matlab when to evaluate what part of the expression. If you are aiming for a better performance, pre-evaluate all constant parts of the function. Let's say your function is @(x)(g(3).*f(x)), in this case matlab would evaluate g(3) on every call.

Instead use:

f=@(x)(x.^2)
g_3=g(3)
h=@(x)(g_3.*f(x))

Now having the constant parts evaluated, you want to see the constants instead of the variabe name. I know two ways to achieve this.

You can use the symbolic toolbox, basically converting the function handle to a symbolic function, then to a function handle again. This not only displays the constants, but also substitutes f. This is not possible for all functions.

>> matlabFunction(h(sym('x')))

ans = 

    @(x)x.^2.*4.2e1

Another possibility is to use eval:

h=eval(['@(x)',sprintf('%e',g_3),'.*f(x)'])

Pre-evaluating constant parts of the expressions as I did in the first step is typically recommendable, but both solutions to get the constant visible in your function handle aren't really recommendable. The first solution using matlabFunction only applies to some functions, while the second comes with all the disadvantages of eval.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How can I use the key name instead of value in map-get?

How can I use a variable's value in a variable name in JavaScript?

How can I get variable name by value in array?

Can't use $refs using variable name instead of direct value

How do i print Variable name instead of the value assigned to the variable?

How can I use a variable's value to look up another variable whose name is said value

in ansible can I use a variable for a module instead of using the module name?

How can I use a variable for a key name?

How can i use $_POST if the name is a variable?

How to use min() and max() functions to print the variable name instead of the value?

How can I use the expanded value of a shell variable in the name of another variable?

How can I use variable value as name for another variable (batch file)?

How can I get rid of a URL in a fetch (to use kind of a variable instead)?

How can I get the value select name

Can I use $name instead of ${name}?

How can I create a variable with a dynamic name based on a string in MATLAB?

How can I use the value of a variable as constant?

How can I use the value of a variable in an IN statement?

how can i print var name instead var value in PHP?

How do i get a property name instead of a property value?

How can I use the name attribute to POST instead of ID for TinyMCE

How can I use table name instead of SCHEMA in PL SQL

How do i use variable's value as a dynamic variable name?

can i use a variable's value as the name of an object in C#

Can I use the value of a variable as a parameter name for a function?

Python get variable name instead of value

In bash, how can I echo the variable name, not the variable value?

How can I tie value of a variable to the name of that variable?

How can I get the name of the team instead of its ID