<?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: Use Dynamic Field References</title>
	<atom:link href="http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/feed/" rel="self" type="application/rss+xml" />
	<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/</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>Thu, 09 Feb 2012 04:19:21 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Brad Stiritz</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32938</link>
		<dc:creator>Brad Stiritz</dc:creator>
		<pubDate>Fri, 27 Jan 2012 21:27:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32938</guid>
		<description>Eric,

Your proposed while-loop reminded me of some of my own code, which might possibly be slightly more readable, due to leveraging regexp(), so I thought I&#039;d post.. 

I wrote a class &#039;FieldInterface&#039; to encapsulate arbitrary-depth struct fields, just as you&#039;re talking about, so I can do things like:

--------------------------------------

A.setField(&#039;A.B.C.D&#039;,20);
ans = A.getField(&#039;A.B.C.D&#039;);

--------------------------------------

The code snip below is the essence of method getField(). Note the reference to classdef constant ROOT_FIELD_NAME, which anchors the &quot;dynamic&quot; Fields. 

--------------------------------------

function outValue = getField(obj,varargin)
...
fieldSpecStr = varargin{1};
...
try
    
    % Construct near-complete field name (only lacking &#039;obj.&#039;), e.g.
    % &#039;ROOT_FIELD_NAME.subStructName.fieldName&#039;
    fullFieldStr = [ obj.ROOT_FIELD_NAME &#039;.&#039; fieldSpecStr ];
    
    % Get cell vector of sub-struct / field names within (fullFieldStr)
    cvFieldSplits = regexp(fullFieldStr,&#039;\.&#039;,&#039;split&#039;);
    
    % Initialize (outValue):
    outValue = obj;
    
    % Iterate through (cvFieldSplits)
    for iSplit = 1 : numel(cvFieldSplits)
    
        % Access next-level within (cvFieldSplits), which will either be a
        % further sub-struct or else the final, desired field:
        outValue = outValue.(cvFieldSplits{iSplit}); 
    end    
    
% Handle failure
catch exception
...
end

--------------------------------------

Hope s/o finds this useful. Any comments appreciated,

Brad

p.s. Loren/Webmaster: I couldn&#039;t get the &quot;pre&quot; tags to work for me, to set off code blocks. Maybe please compare implementation here vs. MATLAB Answers, which uses a slightly different syntax for code blocks, I think.. Also, it would be really convenient if this blog could offer a checkbox to email readers/posters when new comments are added, as does MATLAB Answers &amp; many other forums on the Net..</description>
		<content:encoded><![CDATA[<p>Eric,</p>
<p>Your proposed while-loop reminded me of some of my own code, which might possibly be slightly more readable, due to leveraging regexp(), so I thought I&#8217;d post.. </p>
<p>I wrote a class &#8216;FieldInterface&#8217; to encapsulate arbitrary-depth struct fields, just as you&#8217;re talking about, so I can do things like:</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>A.setField(&#8216;A.B.C.D&#8217;,20);<br />
ans = A.getField(&#8216;A.B.C.D&#8217;);</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>The code snip below is the essence of method getField(). Note the reference to classdef constant ROOT_FIELD_NAME, which anchors the &#8220;dynamic&#8221; Fields. </p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>function outValue = getField(obj,varargin)<br />
&#8230;<br />
fieldSpecStr = varargin{1};<br />
&#8230;<br />
try</p>
<p>    % Construct near-complete field name (only lacking &#8216;obj.&#8217;), e.g.<br />
    % &#8216;ROOT_FIELD_NAME.subStructName.fieldName&#8217;<br />
    fullFieldStr = [ obj.ROOT_FIELD_NAME '.' fieldSpecStr ];</p>
<p>    % Get cell vector of sub-struct / field names within (fullFieldStr)<br />
    cvFieldSplits = regexp(fullFieldStr,&#8217;\.&#8217;,'split&#8217;);</p>
<p>    % Initialize (outValue):<br />
    outValue = obj;</p>
<p>    % Iterate through (cvFieldSplits)<br />
    for iSplit = 1 : numel(cvFieldSplits)</p>
<p>        % Access next-level within (cvFieldSplits), which will either be a<br />
        % further sub-struct or else the final, desired field:<br />
        outValue = outValue.(cvFieldSplits{iSplit});<br />
    end    </p>
<p>% Handle failure<br />
catch exception<br />
&#8230;<br />
end</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>Hope s/o finds this useful. Any comments appreciated,</p>
<p>Brad</p>
<p>p.s. Loren/Webmaster: I couldn&#8217;t get the &#8220;pre&#8221; tags to work for me, to set off code blocks. Maybe please compare implementation here vs. MATLAB Answers, which uses a slightly different syntax for code blocks, I think.. Also, it would be really convenient if this blog could offer a checkbox to email readers/posters when new comments are added, as does MATLAB Answers &amp; many other forums on the Net..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32924</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Thu, 19 Jan 2012 21:45:19 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32924</guid>
		<description>Once again you are awesome!</description>
		<content:encoded><![CDATA[<p>Once again you are awesome!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32494</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 09 Sep 2011 19:38:10 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32494</guid>
		<description>Chris-

You can&#039;t reference a structure that way with multiple field names in one particular position, with or without dynamic field reference.  It always requires a scalar field name.  

--Loren</description>
		<content:encoded><![CDATA[<p>Chris-</p>
<p>You can&#8217;t reference a structure that way with multiple field names in one particular position, with or without dynamic field reference.  It always requires a scalar field name.  </p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chris</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32493</link>
		<dc:creator>Chris</dc:creator>
		<pubDate>Fri, 09 Sep 2011 19:28:27 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32493</guid>
		<description>I&#039;m using dynamic field reference to organize input data into a structure.

here&#039;s a simple example:
B = 

    [1]    &#039;M1&#039;    &#039;test&#039;
    [1]    &#039;M2&#039;    [   2]
[AA ~] = size(B);
for xx = 1:1:AA
    data.(B{xx,2}).time = B{xx,1};
    data.(B{xx,2}).num = B{xx,3};
end

Is there a way to do this without resorting to a look through all the rows of B?  Something closer to:
    data.(B{:,2}).time = B{:,1};
    data.(B{:,2}).num = B{:,3};

Thanks.</description>
		<content:encoded><![CDATA[<p>I&#8217;m using dynamic field reference to organize input data into a structure.</p>
<p>here&#8217;s a simple example:<br />
B = </p>
<p>    [1]    &#8216;M1&#8242;    &#8216;test&#8217;<br />
    [1]    &#8216;M2&#8242;    [   2]<br />
[AA ~] = size(B);<br />
for xx = 1:1:AA<br />
    data.(B{xx,2}).time = B{xx,1};<br />
    data.(B{xx,2}).num = B{xx,3};<br />
end</p>
<p>Is there a way to do this without resorting to a look through all the rows of B?  Something closer to:<br />
    data.(B{:,2}).time = B{:,1};<br />
    data.(B{:,2}).num = B{:,3};</p>
<p>Thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32334</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Mon, 20 Jun 2011 12:12:35 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32334</guid>
		<description>Eric-

You can do what Cris says, but using eval is highly NOT recommended in general.

--Loren</description>
		<content:encoded><![CDATA[<p>Eric-</p>
<p>You can do what Cris says, but using eval is highly NOT recommended in general.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cris Luengo</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32330</link>
		<dc:creator>Cris Luengo</dc:creator>
		<pubDate>Mon, 20 Jun 2011 09:26:09 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32330</guid>
		<description>Eric, why not this?

&lt;pre&gt;
A.l1.l2.l3.l4 = 1234;
fieldname = &#039;l1.l2.l3.l4&#039;;
eval([&#039;A.&#039;,fieldname])
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Eric, why not this?</p>
<pre>
A.l1.l2.l3.l4 = 1234;
fieldname = 'l1.l2.l3.l4';
eval(['A.',fieldname])
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32328</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Fri, 17 Jun 2011 12:00:09 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32328</guid>
		<description>Eric-

I understand the problem you are trying to solve but don&#039;t have any way besides getting the individual fieldnames.  You might put in an enhancement request to get functionality like you describe.  The link for support is on the right side of my blog.

--Loren</description>
		<content:encoded><![CDATA[<p>Eric-</p>
<p>I understand the problem you are trying to solve but don&#8217;t have any way besides getting the individual fieldnames.  You might put in an enhancement request to get functionality like you describe.  The link for support is on the right side of my blog.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32327</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Fri, 17 Jun 2011 11:46:49 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32327</guid>
		<description>Loren,

Let me rephrase.  If I have a structure of arbitrary depth: 

&lt;pre&gt;
A.L1.L2.L3...LN = value;
fullString = &#039;L1.L2.L3...LN&#039;;
&lt;/pre&gt;

is there a way to elegantly access a field given by fullString?  It would be simple enough if the depth if fixed, but the only generalized way I can think is to parse the string and iteratively store substructures until the final result is obtained:

&lt;pre&gt;
A.L1.L2.L3.L4.L5 = 12345;
fullString = &#039;L1.L2.L3.L4.L5&#039;;

dotFind = strfind(fullString,&#039;.&#039;);
temp = A;
while ~isempty(dotFind)
  temp = temp.(fullString(1:dotFind(1)-1));
  fullString = fullString(dotFind(1)+1:end);
  dotFind = strfind(fullString,&#039;.&#039;);
end
FinalAns = temp.(fullString);
disp(FinalAns);
&lt;/pre&gt;

While this works, it is not very elegant, efficient, or readable.  fullString will change in the program.  One time it may be &#039;L1.L2.L3&#039;.  The next it might be &#039;L1.M2.N3.L4&#039;.  Being somewhat new to dynamic field names (thank you, Loren for many other postings elsewhere), I had hoped/expected to just be able to use:  A.(fullString)  which obviously does not work.</description>
		<content:encoded><![CDATA[<p>Loren,</p>
<p>Let me rephrase.  If I have a structure of arbitrary depth: </p>
<pre>
A.L1.L2.L3...LN = value;
fullString = 'L1.L2.L3...LN';
</pre>
<p>is there a way to elegantly access a field given by fullString?  It would be simple enough if the depth if fixed, but the only generalized way I can think is to parse the string and iteratively store substructures until the final result is obtained:</p>
<pre>
A.L1.L2.L3.L4.L5 = 12345;
fullString = 'L1.L2.L3.L4.L5';

dotFind = strfind(fullString,'.');
temp = A;
while ~isempty(dotFind)
  temp = temp.(fullString(1:dotFind(1)-1));
  fullString = fullString(dotFind(1)+1:end);
  dotFind = strfind(fullString,'.');
end
FinalAns = temp.(fullString);
disp(FinalAns);
</pre>
<p>While this works, it is not very elegant, efficient, or readable.  fullString will change in the program.  One time it may be &#8216;L1.L2.L3&#8242;.  The next it might be &#8216;L1.M2.N3.L4&#8242;.  Being somewhat new to dynamic field names (thank you, Loren for many other postings elsewhere), I had hoped/expected to just be able to use:  A.(fullString)  which obviously does not work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Loren</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32324</link>
		<dc:creator>Loren</dc:creator>
		<pubDate>Thu, 16 Jun 2011 14:44:51 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32324</guid>
		<description>Eric-

The last way you note IS the generalized way.

--Loren</description>
		<content:encoded><![CDATA[<p>Eric-</p>
<p>The last way you note IS the generalized way.</p>
<p>&#8211;Loren</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric</title>
		<link>http://blogs.mathworks.com/loren/2005/12/13/use-dynamic-field-references/#comment-32323</link>
		<dc:creator>Eric</dc:creator>
		<pubDate>Thu, 16 Jun 2011 12:55:31 +0000</pubDate>
		<guid isPermaLink="false">http://blogs.mathworks.com/loren-prototype/?p=6#comment-32323</guid>
		<description>Is there a generalized way to access deeper into a structure?  For example,

&lt;pre&gt;
A.l1.l2.l3.l4 = 1234

%would like to do this:
fieldname = &#039;l1.l2.l3.l4&#039;    %next time might be &#039;l1.l2&#039; 
A.(fieldname)
%gives:  ??? Reference to non-existent field &#039;l1.l2.l3.l4&#039;.

A.(&#039;l1&#039;).(&#039;l2&#039;).(&#039;l3&#039;).(&#039;l4&#039;)
%gives correct: 1234

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Is there a generalized way to access deeper into a structure?  For example,</p>
<pre>
A.l1.l2.l3.l4 = 1234

%would like to do this:
fieldname = 'l1.l2.l3.l4'    %next time might be 'l1.l2'
A.(fieldname)
%gives:  ??? Reference to non-existent field 'l1.l2.l3.l4'.

A.('l1').('l2').('l3').('l4')
%gives correct: 1234
</pre>
]]></content:encoded>
	</item>
</channel>
</rss>

