From: bogfrog on 8 Jun 2010 06:40 Hello, Let's say I want to check if the variable s.s1.s2.s3.var exists. I can't use exist() because it says that it does not exist even if it does. I could use isstruct() and isfield() doing something recursively, but it seems like too much work on my part for something so simple. Is there anything in matlab that can easily check that something like s.s1.s2.s3.var exists? Thanks!
From: Alan B on 8 Jun 2010 10:58 bogfrog <aj00mcgraw(a)gmail.com> wrote in message <1473629987.299066.1276008077614.JavaMail.root(a)gallium.mathforum.org>... > Hello, > > Let's say I want to check if the variable > > s.s1.s2.s3.var > > exists. > > I can't use exist() because it says that it does not exist even if it does. > > I could use isstruct() and isfield() doing something recursively, but it seems like too much work on my part for something so simple. > > Is there anything in matlab that can easily check that something like s.s1.s2.s3.var exists? > > Thanks! Why not: isfield(s.s1.s2.s3,'var') ?
From: Steven Lord on 8 Jun 2010 11:04 "bogfrog" <aj00mcgraw(a)gmail.com> wrote in message news:1473629987.299066.1276008077614.JavaMail.root(a)gallium.mathforum.org... > Hello, > > Let's say I want to check if the variable > > s.s1.s2.s3.var > > exists. s.s1.s2.s3.var is NOT a variable. In this case, s is a variable. var is a field of the struct array s3 which is a field of the struct array s2 which is a field of the struct array s1 which is a field of the struct array s. And yes, this distinction is necessary in some circumstances. > I can't use exist() because it says that it does not exist even if it > does. Because EXIST generally only works on _names_, not arbitrary expressions. > I could use isstruct() and isfield() doing something recursively, but it > seems like too much work on my part for something so simple. > > Is there anything in matlab that can easily check that something like > s.s1.s2.s3.var exists? doesVarExist = exist('s', 'var') && isfield(s, 's1') && isfield(s.s1, 's2') && isfield(s.s1.s2, 's3') && isfield(s.s1.s2.s3, 'var') Remember that && short-circuits, so if s doesn't exist then the first clause will return false and the expression will short-circuit. Similarly, if s exists but is not a struct array or doesn't contain a field s1, the second clause will short-circuit. Alternately, you _could_ take a quicker-and-dirtier approach: doesVarExist = true; try s.s1.s2.s3.var; catch doesVarExist = false; end But this assumes that the only reason that the expression in the TRY could fail is because of that nested field failing to exist. You might want to CATCH the MException and check this if you want to be a little slower but a little cleaner. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ To contact Technical Support use the Contact Us link on http://www.mathworks.com
From: bogfrog on 8 Jun 2010 09:10 The try/catch worked perfectly. Thanks! The exist && isfield && ... solution is not viable. I only chose s.s1.s2.s3.var as an example. There are hundreds of unknown possibilities I'm working with.
|
Pages: 1 Prev: stop loop in matrix Next: transfering vars between function |