Thread Subject: Test the existence of a variable

Subject: Test the existence of a variable

From: maxroucool mvjz

Date: 4 Nov, 2009 21:40:23

Message: 1 of 4

Hi all,

I have a really basic problem, but I don't find any solution...
I am doing a function in which you can put 2 arguments 'files' and 'options', but 'options' is not compulsory.
So I want to test its existence, and if the user didn't set any options, so I have to give the default value to 'options'.
Here is my code:

[code]
if exist(options,'var') && strcmp(options.denoised.state, 1)
    if ~isfield(options.denoised,'threshold')
        options.denoised.threshold = 'heursure';
    end
    if ~isfield(options.denoised,'wname')
        options.denoised.wname = 'sym8';
    end
    if ~isfield(options.denoised,'level')
        options.denoised.level = 5;
    end
else
    options.denoised.state = 0; % Bool: Denoise signal or not?
    options.denoised.threshold = 'heursure'; % Can be: rigrsure, heursure, sqtwolog, minimaxi
    options.denoised.wname = 'sym8'; % Many different names: see wfilters()
    options.denoised.level = 5; % Level
end
[/code]

But when I run it, Matlab says me: "??? Undefined function or variable 'options'." !!!
That's wired, isn't it ?!?


Thank you for you help!
+++

Subject: Test the existence of a variable

From: Steven Lord

Date: 4 Nov, 2009 21:45:08

Message: 2 of 4


"maxroucool mvjz" <maxroucool@yahoo.fr> wrote in message
news:hcssc7$3rq$1@fred.mathworks.com...
> Hi all,
>
> I have a really basic problem, but I don't find any solution...
> I am doing a function in which you can put 2 arguments 'files' and
> 'options', but 'options' is not compulsory.
> So I want to test its existence, and if the user didn't set any options,
> so I have to give the default value to 'options'.
> Here is my code:
>
> [code]
> if exist(options,'var') && strcmp(options.denoised.state, 1)

You want the EXIST call to use the _name_ of the variable to be tested, not
the variable itself.

if exist('options', 'var') ...

You may also want to throw in an ISSTRUCT and/or ISFIELD call into the mix
to make sure that options is a struct array with a field denoised that is
itself a struct that has a field state.

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

Subject: Test the existence of a variable

From: jrenfree

Date: 4 Nov, 2009 21:44:49

Message: 3 of 4

On Nov 4, 1:40 pm, "maxroucool mvjz" <maxrouc...@yahoo.fr> wrote:
> Hi all,
>
> I have a really basic problem, but I don't find any solution...
> I am doing a function in which you can put 2 arguments 'files' and 'options', but 'options' is not compulsory.
> So I want to test its existence, and if the user didn't set any options, so I have to give the default value to 'options'.
> Here is my code:
>
> [code]
> if exist(options,'var') && strcmp(options.denoised.state, 1)
>     if ~isfield(options.denoised,'threshold')
>         options.denoised.threshold = 'heursure';
>     end
>     if ~isfield(options.denoised,'wname')
>         options.denoised.wname = 'sym8';
>     end
>     if ~isfield(options.denoised,'level')
>         options.denoised.level = 5;
>     end
> else
>     options.denoised.state = 0;                 % Bool: Denoise signal or not?
>     options.denoised.threshold = 'heursure';    % Can be: rigrsure, heursure, sqtwolog, minimaxi
>     options.denoised.wname = 'sym8';            % Many different names: see wfilters()
>     options.denoised.level = 5;                 % Level
> end
> [/code]
>
> But when I run it, Matlab says me: "??? Undefined function or variable 'options'." !!!
> That's wired, isn't it ?!?
>
> Thank you for you help!
> +++

You need to put ''s around the variable you're looking for. So it
should be:

if exist('options', 'var') ...

Look at help exist for more info.

Subject: Test the existence of a variable

From: maxroucool mvjz

Date: 4 Nov, 2009 22:33:03

Message: 4 of 4

jrenfree <jrenfree@gmail.com> wrote in message <1c9af1f6-9e23-4106-b1d1-96ebef398be2@2g2000prl.googlegroups.com>...
> On Nov 4, 1:40?pm, "maxroucool mvjz" <maxrouc...@yahoo.fr> wrote:
> > Hi all,
> >
> > I have a really basic problem, but I don't find any solution...
> > I am doing a function in which you can put 2 arguments 'files' and 'options', but 'options' is not compulsory.
> > So I want to test its existence, and if the user didn't set any options, so I have to give the default value to 'options'.
> > Here is my code:
> >
> > [code]
> > if exist(options,'var') && strcmp(options.denoised.state, 1)
> > ? ? if ~isfield(options.denoised,'threshold')
> > ? ? ? ? options.denoised.threshold = 'heursure';
> > ? ? end
> > ? ? if ~isfield(options.denoised,'wname')
> > ? ? ? ? options.denoised.wname = 'sym8';
> > ? ? end
> > ? ? if ~isfield(options.denoised,'level')
> > ? ? ? ? options.denoised.level = 5;
> > ? ? end
> > else
> > ? ? options.denoised.state = 0; ? ? ? ? ? ? ? ? % Bool: Denoise signal or not?
> > ? ? options.denoised.threshold = 'heursure'; ? ?% Can be: rigrsure, heursure, sqtwolog, minimaxi
> > ? ? options.denoised.wname = 'sym8'; ? ? ? ? ? ?% Many different names: see wfilters()
> > ? ? options.denoised.level = 5; ? ? ? ? ? ? ? ? % Level
> > end
> > [/code]
> >
> > But when I run it, Matlab says me: "??? Undefined function or variable 'options'." !!!
> > That's wired, isn't it ?!?
> >
> > Thank you for you help!
> > +++
>
> You need to put ''s around the variable you're looking for. So it
> should be:
>
> if exist('options', 'var') ...
>
> Look at help exist for more info.

OK thank to both of you for your rapidity and your efficiency!!

Bye!

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
existence testing maxroucool mvjz 4 Nov, 2009 16:44:05
rssFeed for this Thread

Contact us at files@mathworks.com