<html lang="en"> <head> <title>Manipulating Structures - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Structures.html#Structures" title="Structures"> <link rel="prev" href="Creating-Structures.html#Creating-Structures" title="Creating Structures"> <link rel="next" href="Processing-Data-in-Structures.html#Processing-Data-in-Structures" title="Processing Data in Structures"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="Manipulating-Structures"></a> <p> Next: <a rel="next" accesskey="n" href="Processing-Data-in-Structures.html#Processing-Data-in-Structures">Processing Data in Structures</a>, Previous: <a rel="previous" accesskey="p" href="Creating-Structures.html#Creating-Structures">Creating Structures</a>, Up: <a rel="up" accesskey="u" href="Structures.html#Structures">Structures</a> <hr> </div> <h4 class="subsection">6.1.4 Manipulating Structures</h4> <p>Other functions that can manipulate the fields of a structure are given below. <!-- nfields src/ov-struct.cc --> <p><a name="doc_002dnfields"></a> <div class="defun"> — Built-in Function: <b>nfields</b> (<var>s</var>)<var><a name="index-nfields-444"></a></var><br> <blockquote><p>Return the number of fields of the structure <var>s</var>. </p></blockquote></div> <!-- fieldnames src/ov-struct.cc --> <p><a name="doc_002dfieldnames"></a> <div class="defun"> — Built-in Function: <b>fieldnames</b> (<var>struct</var>)<var><a name="index-fieldnames-445"></a></var><br> <blockquote><p>Return a cell array of strings naming the elements of the structure <var>struct</var>. It is an error to call <code>fieldnames</code> with an argument that is not a structure. </p></blockquote></div> <!-- isfield src/ov-struct.cc --> <p><a name="doc_002disfield"></a> <div class="defun"> — Built-in Function: <b>isfield</b> (<var>x, name</var>)<var><a name="index-isfield-446"></a></var><br> <blockquote><p>Return true if the <var>x</var> is a structure and it includes an element named <var>name</var>. If <var>name</var> is a cell array of strings then a logical array of equal dimension is returned. </p></blockquote></div> <!-- getfield scripts/miscellaneous/getfield.m --> <p><a name="doc_002dgetfield"></a> <div class="defun"> — Function File: [<var>v1</var>, <small class="dots">...</small>] = <b>getfield</b> (<var>s, key, <small class="dots">...</small></var>)<var><a name="index-getfield-447"></a></var><br> <blockquote><p>Extract a field from a structure (or a nested structure). For example: <pre class="example"> ss(1,2).fd(3).b = 5; getfield (ss, {1,2}, "fd", {3}, "b") ⇒ 5 </pre> <p>Note that the function call in the previous example is equivalent to the expression <pre class="example"> i1 = {1,2}; i2 = "fd"; i3 = {3}; i4= "b"; ss(i1{:}).(i2)(i3{:}).(i4) ⇒ 5 </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dsetfield.html#doc_002dsetfield">setfield</a>, <a href="doc_002drmfield.html#doc_002drmfield">rmfield</a>, <a href="doc_002disfield.html#doc_002disfield">isfield</a>, <a href="doc_002disstruct.html#doc_002disstruct">isstruct</a>, <a href="doc_002dfieldnames.html#doc_002dfieldnames">fieldnames</a>, <a href="doc_002dstruct.html#doc_002dstruct">struct</a>. </p></blockquote></div> <!-- setfield scripts/miscellaneous/setfield.m --> <p><a name="doc_002dsetfield"></a> <div class="defun"> — Function File: [<var>k1</var>, <small class="dots">...</small>, <var>v1</var>] = <b>setfield</b> (<var>s, k1, v1, <small class="dots">...</small></var>)<var><a name="index-setfield-448"></a></var><br> <blockquote><p>Set a field member in a (nested) structure array. For example: <pre class="example"> oo(1,1).f0 = 1; oo = setfield (oo, {1,2}, "fd", {3}, "b", 6); oo(1,2).fd(3).b == 6 ⇒ ans = 1 </pre> <p>Note that the same result as in the above example could be achieved by: <pre class="example"> i1 = {1,2}; i2 = "fd"; i3 = {3}; i4 = "b"; oo(i1{:}).(i2)(i3{:}).(i4) == 6 ⇒ ans = 1 </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dgetfield.html#doc_002dgetfield">getfield</a>, <a href="doc_002drmfield.html#doc_002drmfield">rmfield</a>, <a href="doc_002disfield.html#doc_002disfield">isfield</a>, <a href="doc_002disstruct.html#doc_002disstruct">isstruct</a>, <a href="doc_002dfieldnames.html#doc_002dfieldnames">fieldnames</a>, <a href="doc_002dstruct.html#doc_002dstruct">struct</a>. </p></blockquote></div> <!-- rmfield src/ov-struct.cc --> <p><a name="doc_002drmfield"></a> <div class="defun"> — Built-in Function: <b>rmfield</b> (<var>s, f</var>)<var><a name="index-rmfield-449"></a></var><br> <blockquote><p>Return a copy of the structure (array) <var>s</var> with the field <var>f</var> removed. If <var>f</var> is a cell array of strings or a character array, remove the named fields. <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dcellstr.html#doc_002dcellstr">cellstr</a>, <a href="doc_002discellstr.html#doc_002discellstr">iscellstr</a>, <a href="doc_002dsetfield.html#doc_002dsetfield">setfield</a>. </p></blockquote></div> <!-- orderfields scripts/miscellaneous/orderfields.m --> <p><a name="doc_002dorderfields"></a> <div class="defun"> — Function File: [<var>t</var>, <var>p</var>] = <b>orderfields</b> (<var>s1</var>)<var><a name="index-orderfields-450"></a></var><br> — Function File: [<var>t</var>, <var>p</var>] = <b>orderfields</b> (<var>s1, s2</var>)<var><a name="index-orderfields-451"></a></var><br> <blockquote><p>Return a copy of <var>s1</var> with fields arranged alphabetically or as specified by <var>s2</var>. <p>Given one struct, arrange field names in <var>s1</var> alphabetically. <p>If the second argument is a struct, arrange field names in <var>s1</var> as they appear in <var>s2</var>. The second argument may also specify the order in a permutation vector or a cell array of strings containing the fieldnames of <var>s1</var> in the desired order. <p>The optional second output argument <var>p</var> is assigned the permutation vector which converts the original name order into the new name order. <p>Examples: <pre class="example"> s = struct("d", 4, "b", 2, "a", 1, "c", 3); t1 = orderfields (s) ⇒ t1 = { a = 1 b = 2 c = 3 d = 4 } t = struct("d", {}, "c", {}, "b", "a", {}); t2 = orderfields (s, t) ⇒ t2 = { d = 4 c = 3 b = 2 a = 1 } t3 = orderfields (s, [3, 2, 4, 1]); ⇒ t3 = { a = 1 b = 2 c = 3 d = 4 } [t4, p] = orderfields (s, {"d", "c", "b", "a"}) ⇒ t4 = { d = 4 c = 3 b = 2 a = 1 } p = 1 4 2 3 </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dgetfield.html#doc_002dgetfield">getfield</a>, <a href="doc_002drmfield.html#doc_002drmfield">rmfield</a>, <a href="doc_002disfield.html#doc_002disfield">isfield</a>, <a href="doc_002disstruct.html#doc_002disstruct">isstruct</a>, <a href="doc_002dfieldnames.html#doc_002dfieldnames">fieldnames</a>, <a href="doc_002dstruct.html#doc_002dstruct">struct</a>. </p></blockquote></div> <!-- substruct scripts/miscellaneous/substruct.m --> <p><a name="doc_002dsubstruct"></a> <div class="defun"> — Function File: <b>substruct</b> (<var>type, subs, <small class="dots">...</small></var>)<var><a name="index-substruct-452"></a></var><br> <blockquote><p>Create a subscript structure for use with <code>subsref</code> or <code>subsasgn</code>. For example: <pre class="example"> idx = substruct ("()", {3, ":"}) ⇒ idx = { type = () subs = { [1,1] = 3 [1,2] = : } } x = [1, 2, 3; 4, 5, 6; 7, 8, 9]; subsref (x, idx) ⇒ 7 8 9 </pre> <!-- Texinfo @sp should work but in practice produces ugly results for HTML. --> <!-- A simple blank line produces the correct behavior. --> <!-- @sp 1 --> <p class="noindent"><strong>See also:</strong> <a href="doc_002dsubsref.html#doc_002dsubsref">subsref</a>, <a href="doc_002dsubsasgn.html#doc_002dsubsasgn">subsasgn</a>. </p></blockquote></div> </body></html>