How to check variable existence from function ?

How to check variable existence from function ?

Post by John ?str? » Fri, 07 Nov 2003 16:40:10



Hello!

I have a GUI which  has a function of saving a variable as a text file. I'd
like to check first if the variable exists to avoid errors. Exist and who
fucntions cannot be used because they see only the variables in the caller
workspace. Obviously I can't get the variable from the base-workspace using
evalin since  I don't know whether it exists. How to do this ? Thanks!

John

 
 
 

How to check variable existence from function ?

Post by Michael Salloke » Fri, 07 Nov 2003 16:44:04



> Hello!

> I have a GUI which has a function of saving a variable as a text
> file. I'd
> like to check first if the variable exists to avoid errors. Exist
> and who
> fucntions cannot be used because they see only the variables in the
> caller
> workspace. Obviously I can't get the variable from the
> base-workspace using
> evalin since I don't know whether it exists. How to do this ?
> Thanks!

> John

Hello John,

try isvarname.

Michael

 
 
 

How to check variable existence from function ?

Post by John ?str? » Fri, 07 Nov 2003 17:30:38


Hello! isvarname does not do it. It only checks whether the inputted
variable name is valid. It does not tell if a variable with that name
exists.

John



> > Hello!

> > I have a GUI which has a function of saving a variable as a text
> > file. I'd
> > like to check first if the variable exists to avoid errors. Exist
> > and who
> > fucntions cannot be used because they see only the variables in the
> > caller
> > workspace. Obviously I can't get the variable from the
> > base-workspace using
> > evalin since I don't know whether it exists. How to do this ?
> > Thanks!

> > John

> Hello John,

> try isvarname.

> Michael

 
 
 

How to check variable existence from function ?

Post by Michael Salloke » Fri, 07 Nov 2003 17:38:26


Hello John,

how about exist?

Michael

 
 
 

How to check variable existence from function ?

Post by John ?str? » Fri, 07 Nov 2003 17:52:47


As I said in my question "exist" does not work because it only looks for
variables in the caller function workspace (the command are given from a
GUI). The variables I'm looking for are in base workspace in this case. I'd
have to use evalin first to get the variable to caller workspace but if the
variable does not exist, then evalin will produce the error I'm trying to
avoid...

John


Quote:> Hello John,

> how about exist?

> Michael

 
 
 

How to check variable existence from function ?

Post by DO » Fri, 07 Nov 2003 18:03:44



> As I said in my question "exist" does not work because it only looks for
> variables in the caller function workspace (the command are given from a
> GUI). The variables I'm looking for are in base workspace in this case. I'd
> have to use evalin first to get the variable to caller workspace but if the
> variable does not exist, then evalin will produce the error I'm trying to
> avoid...

Why don't you use evalin?

MyVarExists = evalin('base', 'exist(''MayVar'', ''var'');');

-Herbert

 
 
 

How to check variable existence from function ?

Post by John ?str? » Fri, 07 Nov 2003 18:06:38


Thanks a lot! That does it! Somehow I just could figure out to use them both
(evalin and exist) like that...

John



> > As I said in my question "exist" does not work because it only looks for
> > variables in the caller function workspace (the command are given from a
> > GUI). The variables I'm looking for are in base workspace in this case.
I'd
> > have to use evalin first to get the variable to caller workspace but if
the
> > variable does not exist, then evalin will produce the error I'm trying
to
> > avoid...

> Why don't you use evalin?

> MyVarExists = evalin('base', 'exist(''MayVar'', ''var'');');

> -Herbert

 
 
 

1. existence of global variable in Matlab 5?

I am trying to port my suite of mex/m files for mesh generation from
Matlab 4.2 to Matlab 5.1.  There is one incompatibility that I haven't
figured out yet.  In Matlab 4.2, within an m-file, I can check whether a
global variable named 'interactive' exists via the statements:

   global interactive
   if exist('interactive')
     <etc>
   end

This doesn't work in Matlab 5.1: the exist() call ALWAYS returns a
nonzero answer.  In Matlab 5.1, apparently a 'global' declaration
creates the variable.  (This is the case for Matlab 5.1 under Solaris
2.5 and Matlab 5.0 under Window NT 4.0).

How do I achieve the desired effect (testing within an m-file whether a
global variable exists)?

Thanks,

2. LoadImage Needed

3. test for existence of variable?

4. I Wanna Get INK ON MY HANDS!!!....Help Me With All Your Refill Tips!

5. howto check on which functions depend a function?

6. available quantum chemistry programs for ab-initio HF

7. how to check if variable has been defined?

8. I Bought An XBOX

9. How do you check in your code to see if a variable does NOT exist?

10. Check for empty variables...?

11. easiest way to check if variable is complex or real!

12. checking for stability of transfer function

13. Checking syntax of Matlab functions