From: Stefan on
Hello,
Can someone help me on this ?

Suppose I have the following function defined in an m-file.
=============
function grad=testfn(x)

grad=[];
%grad = x
end
=============


The I type the following in command line.
checkfn(@testfn)

In checkfn, I want to test whether grad=[]. "isempty(grad)" doesn't seem to work. Can someone fix it for me please? Thanks
========
function g = checkfn(grad)

if isempty(grad)
%do this
else
%do that
end

end
=======
From: Tim Love on
"Stefan " <akinsakin(a)yahoo.com> writes:

>Hello,
>Can someone help me on this ?

I can't properly answer your question. However, note that

foo=(a)testfn
checkfn(foo(5))
checkfn(foo)

will run your "do this" code then your "do that" code.
From: Jan Simon on
Dear Stefan!

> In checkfn, I want to test whether grad=[]. "isempty(grad)" doesn't seem to work.

"Doesn't seem to work" is not really precise. Did you get an error message? In most cases the error message tells how to fix the problem already. If not, post it with the correspodning line of code.

Kind regards, Jan
From: Steven Lord on

"Stefan " <akinsakin(a)yahoo.com> wrote in message
news:hp24vh$ar5$1(a)fred.mathworks.com...
> Hello,
> Can someone help me on this ?
>
> Suppose I have the following function defined in an m-file.
> =============
> function grad=testfn(x)
>
> grad=[];
> %grad = x
> end
> =============
>
>
> The I type the following in command line.
> checkfn(@testfn)
>
> In checkfn, I want to test whether grad=[]. "isempty(grad)" doesn't seem
> to work. Can someone fix it for me please? Thanks

The code is working correctly. The function handle @testfn is not empty.
If you want to check if the _output_ from the function when you execute it
is empty, then you need to actually invoke the function handle.


function g = checkfn(grad)

% since you've defined grad to take an input
% you need to call it with some input value.
% I chose [] as an arbitrary value
output = grad([]);
if isempty(output)
disp('The function you passed in returned an empty value.');
else
disp('The function you passed in did NOT return an empty value.');
end
g = output;
% etc


--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ


From: Stefan on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hp286u$5vp$1(a)fred.mathworks.com>...
>
> "Stefan " <akinsakin(a)yahoo.com> wrote in message
> news:hp24vh$ar5$1(a)fred.mathworks.com...
> > Hello,
> > Can someone help me on this ?
> >
> > Suppose I have the following function defined in an m-file.
> > =============
> > function grad=testfn(x)
> >
> > grad=[];
> > %grad = x
> > end
> > =============
> >
> >
> > The I type the following in command line.
> > checkfn(@testfn)
> >
> > In checkfn, I want to test whether grad=[]. "isempty(grad)" doesn't seem
> > to work. Can someone fix it for me please? Thanks
>
> The code is working correctly. The function handle @testfn is not empty.
> If you want to check if the _output_ from the function when you execute it
> is empty, then you need to actually invoke the function handle.
>
>
> function g = checkfn(grad)
>
> % since you've defined grad to take an input
> % you need to call it with some input value.
> % I chose [] as an arbitrary value
> output = grad([]);
> if isempty(output)
> disp('The function you passed in returned an empty value.');
> else
> disp('The function you passed in did NOT return an empty value.');
> end
> g = output;
> % etc
>
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>


Thank you Steve. That's exactly what I needed.