Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
function name as input

Subject: function name as input

From: gudny

Date: 18 Apr, 2011 14:30:31

Message: 1 of 5

Hi everyone,
I would like to read my function name from a file, and then call that
function. Example

A = importdata(input)
function_name = A.data(2);
[answer] = function_name(some other input);

does anyone know how this could be achieved?

Best regards,
Gudny

Subject: function name as input

From: J.H.

Date: 18 Apr, 2011 14:32:37

Message: 2 of 5

On Apr 18, 10:30 am, gudny <gud...@gmail.com> wrote:
> Hi everyone,
> I would like to read my function name from a file, and then call that
> function. Example
>
> A = importdata(input)
> function_name = A.data(2);
> [answer] = function_name(some other input);
>
> does anyone know how this could be achieved?
>
> Best regards,
> Gudny

Check out the Eval function.

Subject: function name as input

From: gudny

Date: 18 Apr, 2011 14:46:30

Message: 3 of 5

On Apr 18, 4:32 pm, "J.H." <j.h.nienh...@gmail.com> wrote:
> On Apr 18, 10:30 am, gudny <gud...@gmail.com> wrote:
>
> > Hi everyone,
> > I would like to read my function name from a file, and then call that
> > function. Example
>
> > A = importdata(input)
> > function_name = A.data(2);
> > [answer] = function_name(some other input);
>
> > does anyone know how this could be achieved?
>
> > Best regards,
> > Gudny
>
> Check out the Eval function.

I don't think Eval would help me. At least
[answer] = Eval(function_name)(input)
does not work...

Subject: function name as input

From: Loren Shure

Date: 18 Apr, 2011 15:10:08

Message: 4 of 5


"gudny" <gudnyg@gmail.com> wrote in message
news:af8dd56b-1427-4025-9bff-f39b9bc69062@l36g2000vbp.googlegroups.com...
> Hi everyone,
> I would like to read my function name from a file, and then call that
> function. Example
>
> A = importdata(input)
> function_name = A.data(2);
> [answer] = function_name(some other input);
>
> does anyone know how this could be achieved?
>
> Best regards,
> Gudny

Use str2func to convert the function name to a function handle. Then you
can use the variable storing the function handle to apply the function in
question. Something like this:

fname = 'sinc';
fh = str2func(fname);
y = fh(0:pi/3:5*pi);

--
Loren
http://blogs.mathworks.com/loren/
http://www.mathworks.com/matlabcentral/

Subject: function name as input

From: gudny

Date: 18 Apr, 2011 18:50:34

Message: 5 of 5

On 18 Apr., 17:10, "Loren Shure" <loren.sh...@mathworks.com> wrote:
> "gudny" <gud...@gmail.com> wrote in message
>
> news:af8dd56b-1427-4025-9bff-f39b9bc69062@l36g2000vbp.googlegroups.com...
>
> > Hi everyone,
> > I would like to read my function name from a file, and then call that
> > function. Example
>
> > A = importdata(input)
> > function_name = A.data(2);
> > [answer] = function_name(some other input);
>
> > does anyone know how this could be achieved?
>
> > Best regards,
> > Gudny
>
> Use str2func to convert the function name to a function handle.  Then you
> can use the variable storing the function handle to apply the function in
> question.  Something like this:
>
> fname = 'sinc';
> fh = str2func(fname);
> y = fh(0:pi/3:5*pi);
>
> --
> Lorenhttp://blogs.mathworks.com/loren/http://www.mathworks.com/matlabcentral/

Great! Thank you very much for your help. Not the first time you have
helped me, your posts routinely come up when I google for solutions to
my matlab problems :)

Tags for this Thread

No tags are associated with this thread.

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.

Contact us