Thread Subject: Function handle Problem

Subject: Function handle Problem

From: Rajeev Narayanan

Date: 4 Nov, 2009 15:19:03

Message: 1 of 4

Hi,

I have created the following function and need to be passed as a function handle and i am getting some errors. The parameters T and alpha are not visible in my Top.m. Can someone help me out?

% FileName Parameter.m
function[T, alpha] = Parameter()
T = 0.000001;
alpha = 0.004;
end

%FileName Top.m

fhandle = @Parameter
Q = myfunc(fhandle)

Thanks,
Rajeev

Subject: Function handle Problem

From: Matt

Date: 4 Nov, 2009 16:07:01

Message: 2 of 4

"Rajeev Narayanan" <rite2rajeev@gmail.com> wrote in message <hcs617$2d9$1@fred.mathworks.com>...
> Hi,
>
> I have created the following function and need to be passed as a function handle and i am getting some errors. The parameters T and alpha are not visible in my Top.m.
=====

Why should they be visible? There is nothing in your code below to indicate that T and alpha are generated inside Top.m


> % FileName Parameter.m
> function[T, alpha] = Parameter()
> T = 0.000001;
> alpha = 0.004;
> end
>
> %FileName Top.m
>
> fhandle = @Parameter
> Q = myfunc(fhandle)
>
> Thanks,
> Rajeev

Subject: Function handle Problem

From: Rajeev Narayanan

Date: 4 Nov, 2009 16:14:04

Message: 3 of 4

Hi,

Thanks for the reply. I think i didnt make it clear in my question. I have one more fucntion myfunc where i am using the parameter T and alpha. By passing it through the handle, the error i am getting is "T not defined"


% FileName Parameter.m
function[T, alpha] = Parameter()
T = 0.000001;
alpha = 0.004;
end
 
%FileName Top.m
 fhandle = @Parameter
 Q = myfunc(fhandle)
 
% FileName myfunc.m

function[X] = myfunc(T, alpha)

X = T/alpha ;

end


Thanks,
 Rajeev



















"Rajeev Narayanan" <rite2rajeev@gmail.com> wrote in message <hcs617$2d9$1@fred.mathworks.com>...
> Hi,
>
> I have created the following function and need to be passed as a function handle and i am getting some errors. The parameters T and alpha are not visible in my Top.m. Can someone help me out?
>
> % FileName Parameter.m
> function[T, alpha] = Parameter()
> T = 0.000001;
> alpha = 0.004;
> end
>
> %FileName Top.m
>
> fhandle = @Parameter
> Q = myfunc(fhandle)
>
> Thanks,
> Rajeev

Subject: Function handle Problem

From: Matt

Date: 4 Nov, 2009 16:30:20

Message: 4 of 4

"Rajeev Narayanan" <rite2rajeev@gmail.com> wrote in message <hcs98c$5q$1@fred.mathworks.com>...
> Hi,
>
> Thanks for the reply. I think i didnt make it clear in my question. I have one more fucntion myfunc where i am using the parameter T and alpha. By passing it through the handle, the error i am getting is "T not defined"
>
>
> % FileName Parameter.m
> function[T, alpha] = Parameter()
> T = 0.000001;
> alpha = 0.004;
> end
>
> %FileName Top.m
> fhandle = @Parameter
> Q = myfunc(fhandle)
>
> % FileName myfunc.m
>
> function[X] = myfunc(T, alpha)
>
> X = T/alpha ;
>
> end
>

I think this is the definition of myfunc that you really want:

function [X] = myfunc(fhandle)

 [T,alpha]=fhandle();
  X = T/alpha ;

end

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 at files@mathworks.com