<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Evolution of the Function isfield</title>
	<atom:link href="http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/</link>
	<description>Loren Shure works on design of the MATLAB language at MathWorks. She writes here about once a week on MATLAB programming and related topics.</description>
	<lastBuildDate>Wed, 15 Feb 2012 15:23:22 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-30266</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 01 May 2009 19:17:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-30266</guid>
		<description>Kieran-

Use the tags pre and /pre inside angle brackets to denote preformatted code.  I fixed that for your last comment.

-Loren</description>
		<content:encoded><![CDATA[<p>Kieran-</p>
<p>Use the tags pre and /pre inside angle brackets to denote preformatted code.  I fixed that for your last comment.</p>
<p>-Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Kieran Parsons</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-30265</link>
		<dc:creator>Kieran Parsons</dc:creator>
		<pubDate>Fri, 01 May 2009 15:57:26 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-30265</guid>
		<description>Hi Loren,

I came across this old post while trying to figure out why isfield() does not work on objects in 2009a. I think that since fieldnames() does work on an object that isfield() should do as well (without needing to overload isfield for the specific classes). I have added a feature request. In the meantime I came up with the following, based on your code above, while also adding cell field support:

&lt;pre&gt;
function tf = isfield2(s, field)
%ISFIELD2 Adds object support to isfield.
%   TF = ISFIELD2(S, FIELD) checks if FIELD is a field of S (a structure or an object).
%
%   See also ISFIELD.
%
if isstruct(s)
  tf = isfield(s, field);
elseif isobject(s)
  tf = false;
  if iscell(field)
    tf = false(size(field));
    for idx = 1:numel(tf)
      try
        l = lasterror;
        tmp = s.(field{idx}); 
        tf(idx) = true;
      catch 
        lasterror(l);
      end
    end
  else
    try
      l = lasterror;
      tmp = s.(field);
      tf = true;
    catch 
      lasterror(l);
    end
  end
end
end
&lt;/pre&gt;

While writing this I also found out that true/false can take a size argument which I never knew before.

Can you please point me to how to provide formatted code in these blog posts. I could not find this info on the blog page.

Thanks,
Kieran</description>
		<content:encoded><![CDATA[<p>Hi Loren,</p>
<p>I came across this old post while trying to figure out why isfield() does not work on objects in 2009a. I think that since fieldnames() does work on an object that isfield() should do as well (without needing to overload isfield for the specific classes). I have added a feature request. In the meantime I came up with the following, based on your code above, while also adding cell field support:</p>
<pre>
function tf = isfield2(s, field)
%ISFIELD2 Adds object support to isfield.
%   TF = ISFIELD2(S, FIELD) checks if FIELD is a field of S (a structure or an object).
%
%   See also ISFIELD.
%
if isstruct(s)
  tf = isfield(s, field);
elseif isobject(s)
  tf = false;
  if iscell(field)
    tf = false(size(field));
    for idx = 1:numel(tf)
      try
        l = lasterror;
        tmp = s.(field{idx});
        tf(idx) = true;
      catch
        lasterror(l);
      end
    end
  else
    try
      l = lasterror;
      tmp = s.(field);
      tf = true;
    catch
      lasterror(l);
    end
  end
end
end
</pre>
<p>While writing this I also found out that true/false can take a size argument which I never knew before.</p>
<p>Can you please point me to how to provide formatted code in these blog posts. I could not find this info on the blog page.</p>
<p>Thanks,<br />
Kieran</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-23553</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Wed, 19 Dec 2007 22:24:30 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-23553</guid>
		<description>Priyantha-

This question is not relevant to this article.  Please contact technical support.

--Loren</description>
		<content:encoded><![CDATA[<p>Priyantha-</p>
<p>This question is not relevant to this article.  Please contact technical support.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Priyantha</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-23552</link>
		<dc:creator>Priyantha</dc:creator>
		<pubDate>Wed, 19 Dec 2007 22:17:59 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-23552</guid>
		<description>When I tried to assign a file using the following sequence, I got the message
Attempt to reference field of non-structure array.

load M:\WCCT_05.dat;
wc=WCCT_05.dat

What does this mean?</description>
		<content:encoded><![CDATA[<p>When I tried to assign a file using the following sequence, I got the message<br />
Attempt to reference field of non-structure array.</p>
<p>load M:\WCCT_05.dat;<br />
wc=WCCT_05.dat</p>
<p>What does this mean?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-13270</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 09 Oct 2006 18:40:25 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-13270</guid>
		<description>Gary-

I think it was built into R14sp3.

--Loren</description>
		<content:encoded><![CDATA[<p>Gary-</p>
<p>I think it was built into R14sp3.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gary Roth</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-13267</link>
		<dc:creator>Gary Roth</dc:creator>
		<pubDate>Mon, 09 Oct 2006 18:21:23 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-13267</guid>
		<description>Loren,
Is the rewrite of isfield something that is new for 2006b, or was it implemented earlier?</description>
		<content:encoded><![CDATA[<p>Loren,<br />
Is the rewrite of isfield something that is new for 2006b, or was it implemented earlier?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-12796</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 02 Oct 2006 12:14:56 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-12796</guid>
		<description>Oops, I misunderstood Duane&#039;s comments.  You are right that the statements could be combined.

--Loren</description>
		<content:encoded><![CDATA[<p>Oops, I misunderstood Duane&#8217;s comments.  You are right that the statements could be combined.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-12785</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 02 Oct 2006 02:06:14 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-12785</guid>
		<description>Duane, Charles-

Thanks to both of you for great comments.

The reason we had the initial if statement in the original isfield was so that if/when MATLAB allows subclassing of built-in classes, such as struct, an object derived from struct might return true for a field that was not meant to be public, unless the class designer also overloaded isfield.

The reason for the two exit paths is to not waste any time doing calculations for the non-struct if it was an object were subsref had been overloaded to allow dot-referencing.  If that happened, I wanted to be sure the class designer had to intentionally overload isfield to be sure which fields to make public.  It&#039;s possible that this is overkill.  But it does guarantee that the M-file version behaves identically to the built-in one, and is exactly the design we intended.

--Loren</description>
		<content:encoded><![CDATA[<p>Duane, Charles-</p>
<p>Thanks to both of you for great comments.</p>
<p>The reason we had the initial if statement in the original isfield was so that if/when MATLAB allows subclassing of built-in classes, such as struct, an object derived from struct might return true for a field that was not meant to be public, unless the class designer also overloaded isfield.</p>
<p>The reason for the two exit paths is to not waste any time doing calculations for the non-struct if it was an object were subsref had been overloaded to allow dot-referencing.  If that happened, I wanted to be sure the class designer had to intentionally overload isfield to be sure which fields to make public.  It&#8217;s possible that this is overkill.  But it does guarantee that the M-file version behaves identically to the built-in one, and is exactly the design we intended.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Oliver A. Chapman, PE</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-12684</link>
		<dc:creator>Oliver A. Chapman, PE</dc:creator>
		<pubDate>Thu, 28 Sep 2006 18:34:43 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-12684</guid>
		<description>Loren,

Thanks for sharing the evolution of this code, it is a good series of examples on programming style.

However, I don&#039;t like the two exit paths in your last bit of code.  I would have used an if / else / end approach.

It seems like Duane&#039;s code is even simpler and just as easy to follow.  Although, maybe it has the same performance issues that you discussed.

But, in answer to your question, I&#039;ve been lucky in that I&#039;ve been able to come down on the side of easy to read code and sacrifice performance.  So, I would have used the first set of code and looked for other ways to get the performance like faster or more computers, compiling the code, breaking the problem up into smaller pieces or hiring more people to manage the running.  So, no, I haven&#039;t had to save and reset this type of variable, yet.

But still, I&#039;m happy to see these examples.</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>Thanks for sharing the evolution of this code, it is a good series of examples on programming style.</p>
<p>However, I don&#8217;t like the two exit paths in your last bit of code.  I would have used an if / else / end approach.</p>
<p>It seems like Duane&#8217;s code is even simpler and just as easy to follow.  Although, maybe it has the same performance issues that you discussed.</p>
<p>But, in answer to your question, I&#8217;ve been lucky in that I&#8217;ve been able to come down on the side of easy to read code and sacrifice performance.  So, I would have used the first set of code and looked for other ways to get the performance like faster or more computers, compiling the code, breaking the problem up into smaller pieces or hiring more people to manage the running.  So, no, I haven&#8217;t had to save and reset this type of variable, yet.</p>
<p>But still, I&#8217;m happy to see these examples.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Duane Hanselman</title>
		<link>http://blogs.mathworks.com/loren/2006/09/27/evolution-of-the-function-isfield/#comment-12681</link>
		<dc:creator>Duane Hanselman</dc:creator>
		<pubDate>Thu, 28 Sep 2006 10:40:16 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren/?p=56#comment-12681</guid>
		<description>I never liked the original isfield because it didn&#039;t need the if statement. Just combine the if test with the logical statement:

tf = isa(s,&#039;struct&#039;) &amp;&amp; ...
     any(strcmp(fieldnames(s),fn));

I am glad to see isfield as a built in, so this is gone.

The other one that puzzles me is

if ~isempty(msg)
   error(msg)
end
This one used to appear in dozens of places and perhaps still does. It can be simply replaced by

error(msg)

since error does nothing with an empty argument.

Thanks for doing this blog. It is a wonderful educational tool.</description>
		<content:encoded><![CDATA[<p>I never liked the original isfield because it didn&#8217;t need the if statement. Just combine the if test with the logical statement:</p>
<p>tf = isa(s,&#8217;struct&#8217;) &amp;&amp; &#8230;<br />
     any(strcmp(fieldnames(s),fn));</p>
<p>I am glad to see isfield as a built in, so this is gone.</p>
<p>The other one that puzzles me is</p>
<p>if ~isempty(msg)<br />
   error(msg)<br />
end<br />
This one used to appear in dozens of places and perhaps still does. It can be simply replaced by</p>
<p>error(msg)</p>
<p>since error does nothing with an empty argument.</p>
<p>Thanks for doing this blog. It is a wonderful educational tool.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

