Thread Subject: check if variable exists

Subject: check if variable exists

From: Adam Chapman

Date: 29 Dec, 2008 17:50:47

Message: 1 of 9

Hi,

I want to check if a variable exists before i make a decision to add
data to that variable, or create the variable if it does nopt already
exist.

in very simple terms:

if A exists
    A=A+data
else
    A=data
end

I expect my data will be in structure format, but haven't decided yet,
as cell format might be better.

my question is how do you check if the variable exists or not? all the
data will be stored in a .mat file, so i will probably have to load it
at the start of the script. If there is a way to check if variables
exist within a mat file without having to load it i would be
interested too.

Thanks
Adam

Subject: check if variable exists

From: Nasser Abbasi

Date: 29 Dec, 2008 18:08:23

Message: 2 of 9


"Adam Chapman" <adam.chapman@student.manchester.ac.uk> wrote in message
news:d0fada10-feff-42f8-
>
> my question is how do you check if the variable exists or not?

help exist ?

Subject: check if variable exists

From: David

Date: 29 Dec, 2008 18:10:03

Message: 3 of 9

Adam Chapman <adam.chapman@student.manchester.ac.uk> wrote in message <d0fada10-feff-42f8-a7dc-64e891aab7c4@i18g2000prf.googlegroups.com>...
> Hi,
>
> I want to check if a variable exists before i make a decision to add
> data to that variable, or create the variable if it does nopt already
> exist.
>
> in very simple terms:
>
> if A exists
> A=A+data
> else
> A=data
> end
>
> I expect my data will be in structure format, but haven't decided yet,
> as cell format might be better.
>
> my question is how do you check if the variable exists or not? all the
> data will be stored in a .mat file, so i will probably have to load it
> at the start of the script. If there is a way to check if variables
> exist within a mat file without having to load it i would be
> interested too.
>
> Thanks
> Adam

doc exist
doc isfield

the load function can be told to only load specific variables from a mat file, you could then check to see if they exist to see if they were in the file... i don't know of any way to query a mat file otherwise.
i

Subject: check if variable exists

From: Loren Shure

Date: 29 Dec, 2008 18:13:16

Message: 4 of 9

In article <d0fada10-feff-42f8-a7dc-64e891aab7c4
@i18g2000prf.googlegroups.com>, adam.chapman@student.manchester.ac.uk
says...
> Hi,
>
> I want to check if a variable exists before i make a decision to add
> data to that variable, or create the variable if it does nopt already
> exist.
>
> in very simple terms:
>
> if A exists
> A=A+data
> else
> A=data
> end
>
> I expect my data will be in structure format, but haven't decided yet,
> as cell format might be better.
>
> my question is how do you check if the variable exists or not? all the
> data will be stored in a .mat file, so i will probably have to load it
> at the start of the script. If there is a way to check if variables
> exist within a mat file without having to load it i would be
> interested too.
>
> Thanks
> Adam
>

help exist


--
Loren
http://blogs.mathworks.com/loren

Subject: check if variable exists

From: Adam Chapman

Date: 29 Dec, 2008 18:35:03

Message: 5 of 9

On Dec 29, 6:13=A0pm, Loren Shure <lo...@mathworks.com> wrote:
> In article <d0fada10-feff-42f8-a7dc-64e891aab7c4
> @i18g2000prf.googlegroups.com>, adam.chap...@student.manchester.ac.uk
> says...
>
>
>
>
>
> > Hi,
>
> > I want to check if a variable exists before i make a decision to add
> > data to that variable, or create the variable if it does nopt already
> > exist.
>
> > in very simple terms:
>
> > if A exists
> > =A0 =A0 A=3DA+data
> > else
> > =A0 =A0 A=3Ddata
> > end
>
> > I expect my data will be in structure format, but haven't decided yet,
> > as cell format might be better.
>
> > my question is how do you check if the variable exists or not? all the
> > data will be stored in a .mat file, so i will probably have to load it
> > at the start of the script. If there is a way to check if variables
> > exist within a mat file without having to load it i would be
> > interested too.
>
> > Thanks
> > Adam
>
> help exist
>
> --
> Lorenhttp://blogs.mathworks.com/loren- Hide quoted text -
>
> - Show quoted text -


got it:
exist('varname','var')

didn't notice the 'var' bit in the help in my stressed out state.
Thanks.

Subject: check if variable exists

From: Matt

Date: 29 Dec, 2008 18:41:02

Message: 6 of 9

Adam Chapman <adam.chapman@student.manchester.ac.uk> wrote in message <d0fada10-feff-42f8-a7dc-64e891aab7c4@i18g2000prf.googlegroups.com>...
> Hi,
>
> I want to check if a variable exists before i make a decision to add
> data to that variable, or create the variable if it does nopt already
> exist.
>
> in very simple terms:
>
> if A exists
> A=A+data
> else
> A=data
> end
>
> I expect my data will be in structure format, but haven't decided yet,
> as cell format might be better.
>
> my question is how do you check if the variable exists or not? all the
> data will be stored in a .mat file, so i will probably have to load it
> at the start of the script. If there is a way to check if variables
> exist within a mat file without having to load it i would be
> interested too.
>
> Thanks
> Adam

Another option to consider,

 try
     A=A+data
 catch
    A=data
end

Subject: check if variable exists

From: Doug Schwarz

Date: 29 Dec, 2008 19:25:28

Message: 7 of 9

In article
<d0fada10-feff-42f8-a7dc-64e891aab7c4@i18g2000prf.googlegroups.com>,
 Adam Chapman <adam.chapman@student.manchester.ac.uk> wrote:

[snip]

> If there is a way to check if variables
> exist within a mat file without having to load it i would be
> interested too.


  filename = 'myfile.mat';
  w = whos('-file',filename);

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: check if variable exists

From: Steven Lord

Date: 31 Dec, 2008 06:01:09

Message: 8 of 9


"Adam Chapman" <adam.chapman@student.manchester.ac.uk> wrote in message
news:d0fada10-feff-42f8-a7dc-64e891aab7c4@i18g2000prf.googlegroups.com...
> Hi,
>
> I want to check if a variable exists before i make a decision to add
> data to that variable, or create the variable if it does nopt already
> exist.
>
> in very simple terms:
>
> if A exists
> A=A+data
> else
> A=data
> end
>
> I expect my data will be in structure format, but haven't decided yet,
> as cell format might be better.

Others have mentioned EXIST, but if your data is in structure format you may
also need ISSTRUCT and ISFIELD.

*snip*

--
Steve Lord
slord@mathworks.com

Subject: check if variable exists

From: Adam Chapman

Date: 2 Jan, 2009 22:23:15

Message: 9 of 9

On Dec 31 2008, 6:01=A0am, "Steven Lord" <sl...@mathworks.com> wrote:
> "Adam Chapman" <adam.chap...@student.manchester.ac.uk> wrote in message
>
> news:d0fada10-feff-42f8-a7dc-64e891aab7c4@i18g2000prf.googlegroups.com...
>
>
>
>
>
> > Hi,
>
> > I want to check if a variable exists before i make a decision to add
> > data to that variable, or create the variable if it does nopt already
> > exist.
>
> > in very simple terms:
>
> > if A exists
> > =A0 =A0A=3DA+data
> > else
> > =A0 =A0A=3Ddata
> > end
>
> > I expect my data will be in structure format, but haven't decided yet,
> > as cell format might be better.
>
> Others have mentioned EXIST, but if your data is in structure format you =
may
> also need ISSTRUCT and ISFIELD.
>
> *snip*
>
> --
> Steve Lord
> sl...@mathworks.com- Hide quoted text -
>
> - Show quoted text -

awesome! thanks fro the extra help there
Adam

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
exist David 29 Dec, 2008 13:15:05
isfield David 29 Dec, 2008 13:15:05
load David 29 Dec, 2008 13:15:05
rssFeed for this Thread

Contact us at files@mathworks.com