Thread Subject: input problems: beginner in matlab

Subject: input problems: beginner in matlab

From: Eddy

Date: 17 Sep, 2007 20:50:05

Message: 1 of 4

x=[1:1:10]
fprintf('please enter a function like sin cos exp etc,\n')
fprintf('the calculation performed will be like
pfun(funct(x)),\n')
fprintf('where funct is your function and x= 0 to 10
increment with 1,\n')
funct=input('function funct=','s')
pfun=funct'(x)'

In the technical course we are starting to learn Matlab for
signal processing.
So now we have to do all kinds of exercises, and I do like it.
But one thing does not fall in to place.
In the above script I prompt for a input from the user and
store this in a variable 'funct' the input is as a string.
now I want to use what is in the string to doe some
computing but this last step will not succeed.
So if the input would be sin the computation should be sin(x)

Can somebody help me with this probably syntax problem??

thanks

Eddy

Subject: input problems: beginner in matlab

From: Travis

Date: 17 Sep, 2007 21:04:38

Message: 2 of 4

Is this what you mean?

x=[1:1:10];
funct = input('function to evaluate = ','s');
to_evaluate = [funct '(x)'];
y = eval(to_evaluate)

(should work if you input the correct built in matlab
function names... ie sin, cos, tan, etc...)

Might be a better way to do it, but I think that might work.

--Travis

"Eddy " <grotehaas@mathworks.com> wrote in message
<fcmp9t$3vj$1@fred.mathworks.com>...
> x=[1:1:10]
> fprintf('please enter a function like sin cos exp etc,\n')
> fprintf('the calculation performed will be like
> pfun(funct(x)),\n')
> fprintf('where funct is your function and x= 0 to 10
> increment with 1,\n')
> funct=input('function funct=','s')
> pfun=funct'(x)'
>
> In the technical course we are starting to learn Matlab for
> signal processing.
> So now we have to do all kinds of exercises, and I do like it.
> But one thing does not fall in to place.
> In the above script I prompt for a input from the user and
> store this in a variable 'funct' the input is as a string.
> now I want to use what is in the string to doe some
> computing but this last step will not succeed.
> So if the input would be sin the computation should be sin(x)
>
> Can somebody help me with this probably syntax problem??
>
> thanks
>
> Eddy

Subject: input problems: beginner in matlab

From: Eddy

Date: 17 Sep, 2007 21:30:06

Message: 3 of 4

yes that's what I mean,
thanks for your help Travis,

Eddy

"Travis " <traviib.nospam.@yahoo.com> wrote in message
<fcmq56$i9h$1@fred.mathworks.com>...
> Is this what you mean?
>
> x=[1:1:10];
> funct = input('function to evaluate = ','s');
> to_evaluate = [funct '(x)'];
> y = eval(to_evaluate)
>
> (should work if you input the correct built in matlab
> function names... ie sin, cos, tan, etc...)
>
> Might be a better way to do it, but I think that might work.
>
> --Travis
>
> "Eddy " <grotehaas@mathworks.com> wrote in message
> <fcmp9t$3vj$1@fred.mathworks.com>...
> > x=[1:1:10]
> > fprintf('please enter a function like sin cos exp etc,\n')
> > fprintf('the calculation performed will be like
> > pfun(funct(x)),\n')
> > fprintf('where funct is your function and x= 0 to 10
> > increment with 1,\n')
> > funct=input('function funct=','s')
> > pfun=funct'(x)'
> >
> > In the technical course we are starting to learn Matlab for
> > signal processing.
> > So now we have to do all kinds of exercises, and I do
like it.
> > But one thing does not fall in to place.
> > In the above script I prompt for a input from the user and
> > store this in a variable 'funct' the input is as a string.
> > now I want to use what is in the string to doe some
> > computing but this last step will not succeed.
> > So if the input would be sin the computation should be
sin(x)
> >
> > Can somebody help me with this probably syntax problem??
> >
> > thanks
> >
> > Eddy
>

Subject: input problems: beginner in matlab

From: Nick Denman

Date: 18 Sep, 2007 03:02:33

Message: 4 of 4

Hi Eddy

"Eddy " <grotehaas@mathworks.com> wrote in message
<fcmp9t$3vj$1@fred.mathworks.com>...
> x=[1:1:10]
> fprintf('please enter a function like sin cos exp
etc,\n')
> fprintf('the calculation performed will be like
> pfun(funct(x)),\n')
> fprintf('where funct is your function and x= 0 to 10
> increment with 1,\n')
> funct=input('function funct=','s')
> pfun=funct'(x)'

help str2func

E.g.
x = 1:10;
str = 'sin';
fcn = str2func(str);
fcn(x)

--
Nick

Tags for this Thread

Everyone's Tags:

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.

Tag Activity for This Thread
Tag Applied By Date/Time
str2func Nick Denman 17 Sep, 2007 23:05:10
rssFeed for this Thread

Contact us at files@mathworks.com