Thread Subject:
Extracting an element from an array and turning it into a function handle

Subject: Extracting an element from an array and turning it into a function handle

From: Katherine Varela

Date: 1 May, 2013 01:31:10

Message: 1 of 2

Hello!
Can someone please show me how to turn the element of an array that is returned by a function, into a function handle. For example, suppose I was given the function "randfun" :

"function [ fval, gval ] = randfun( x, y )

fval = x^2 + y;
gval = x^3 + x + 6*y;

end"

When I call the function I would have to feed it numerical values x and y, and
I would receive numerical values for fval and gval. But what I wish to do is create function handles fval(x,y) and gval(x,y) from the function randfun(x,y) (i.e I cannot access fval and gval directly).

I've tried doing the following

M = @(x,y){randfun(x,y)}

But it only assigns M to be the function handle for fval and I do not know how to make gval a function handle from just having access to "randfun".

PS the function randfun I used here is an oversimplified example of the function I need to use. In general, what I do not know how to do is take the two outputs of the given function and turn them into function handles that I can then feed to another function that only takes function handles. Thanks, I appreciate any help!

-Katherine

Subject: Extracting an element from an array and turning it into a function handle

From: Bruno Luong

Date: 1 May, 2013 12:00:09

Message: 2 of 2

"Katherine Varela" wrote in message <klpr8u$got$1@newscl01ah.mathworks.com>...
> Hello!
> Can someone please show me how to turn the element of an array that is returned by a function, into a function handle. For example, suppose I was given the function "randfun" :
>
> "function [ fval, gval ] = randfun( x, y )
>
> fval = x^2 + y;
> gval = x^3 + x + 6*y;
>
> end"
>
> When I call the function I would have to feed it numerical values x and y, and
> I would receive numerical values for fval and gval. But what I wish to do is create function handles fval(x,y) and gval(x,y) from the function randfun(x,y) (i.e I cannot access fval and gval directly).
>

Not sure I fully understand your need, but what's wrong with putting those lines in 2 mfiles

function fval = ffun (x,y)
[fval,~] = randfun(x,y);
end

function gval = gfun (x,y)
[~,gval] = randfun(x,y);
end

Then @ffun and @gfun are function handles that return respectively the first and second output.

Bruno

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us