{"id":6,"date":"2005-12-13T14:27:41","date_gmt":"2005-12-13T19:27:41","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=6"},"modified":"2005-12-30T15:34:50","modified_gmt":"2005-12-30T20:34:50","slug":"use-dynamic-field-references","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2005\/12\/13\/use-dynamic-field-references\/","title":{"rendered":"Use Dynamic Field References"},"content":{"rendered":"<p>Use dynamic field references, the notation <span class=\"code\">.()<\/span>, where possible, instead of <kbd>setfield<\/kbd>, <kbd>getfield<\/kbd>, and <kbd>eval<\/kbd>.<\/p>\n<h3>History<\/h3>\n<p>In MATLAB 6.5 (Release 13), we introduced dynamic field references into MATLAB.  What are they and why should you use them?<\/p>\n<p>In MATLAB, there are 3 basic types of arrays, traditional ones where you index using parentheses <kbd>()<\/kbd>, cell arrays where you can address the contents using curly braces <kbd>{}<\/kbd>, and structures (known in MATLAB as the type <kbd>struct<\/kbd>) where you access elements using the dot <kbd>.<\/kbd> notation.<\/p>\n<p>Before R13, if you had collected information into a variable, call it <kbd>ind<\/kbd>, about which parts of an array you wanted to either index into or to change, you could do so using the variable in a straight-forward manner - <em>if<\/em> your array was a regular or cell array.  By that, I mean that you could do things like:<\/p>\n<pre class=\"code\">\r\ny = x(ind)\r\nx(ind) = 17\r\n       \r\n% With cells, you typically access the contents\r\n% of one cell at a time.\r\nc{ind(1)} = x(ind(1))\r\n<\/pre>\n<p>However, there was no symmetry with how to accomplish the same task if your array was a <kbd>struct<\/kbd> and you had the fieldnames in a variable; instead you had to use <kbd>setfield<\/kbd> and <kbd>getfield<\/kbd>.  There was extra overhead for accessing structure elements compared to other array types.  And that's why dynamic field indexing was introduced.<\/p>\n<h3>Example<\/h3>\n<h4>Preferred:<\/h4>\n<pre class=\"code\">\r\nfldnm = 'fred';\r\ns.(fldnm) = 18;\r\ny = s.(fldnm)\r\n<\/pre>\n<h4>Not Recommended:<\/h4>\n<pre class=\"code\">\r\ns  =  setfield(s,'fldnm',18);\r\ny = getfield(s,'fldnm');\r\n\r\neval(['s.' fldnm ' = 18']);\r\neval(['y = s.',fldnm])\r\n<\/pre>\n<h3>Benefits<\/h3>\n<ul>\n<li>Speed - when you can use <kbd>.()<\/kbd>, it is much faster.  Part of the reason is because you don't have the overhead of calling yet another function, i.e., <kbd>setfield<\/kbd> or <kbd>getfield<\/kbd>.  An additional reason is that MATLAB can work on the structure field in place and unnecessary copies are not made.   Contrast these two cases, using <kbd>setfield<\/kbd> and using dynamic field referencing:\n<pre class=\"code\">\r\ns = setfield(s,fldnm,3);\r\ns.(fldnm) = 3;\r\n<\/pre>\n<p>The first line requires at least 2 copies of the structure <kbd>s<\/kbd> while the second line requires space for only one instance.\n<\/li>\n<li>Readability -- code is easier to understand since you can see the components of the structure.<\/li>\n<li>Analyzability - programs that use <kbd>eval<\/kbd> can be particularly hard to analyze, for example for dependency reports.<\/li>\n<\/ul>\n<h3>Contraindications<\/h3>\n<ul>\n<li>There are certain very complex calling sequences for <kbd>setfield<\/kbd> that can't be translated into <kbd>.()<\/kbd> notation easily.<\/li>\n<li>If you don't know the overall \"structure\" of your struct (e.g., all names at one \"level\"), it's hard to program generically with <kbd>.()<\/kbd><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Use dynamic field references, the notation .(), where possible, instead of setfield, getfield, and eval.<br \/>\nHistory<br \/>\nIn MATLAB 6.5 (Release 13), we introduced dynamic field references into MATLAB.  What... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2005\/12\/13\/use-dynamic-field-references\/\">read more >><\/a><\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[4,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/6"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}