<html lang="en"> <head> <title>Creating Structures - Untitled</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="Untitled"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Data-Structures.html#Data-Structures" title="Data Structures"> <link rel="prev" href="Structure-Arrays.html#Structure-Arrays" title="Structure Arrays"> <link rel="next" href="Manipulating-Structures.html#Manipulating-Structures" title="Manipulating 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="Creating-Structures"></a> <p> Next: <a rel="next" accesskey="n" href="Manipulating-Structures.html#Manipulating-Structures">Manipulating Structures</a>, Previous: <a rel="previous" accesskey="p" href="Structure-Arrays.html#Structure-Arrays">Structure Arrays</a>, Up: <a rel="up" accesskey="u" href="Data-Structures.html#Data-Structures">Data Structures</a> <hr> </div> <h4 class="subsection">6.1.3 Creating Structures</h4> <p>As well as indexing a structure with ".", Octave can create a structure with the <code>struct</code> command. <code>struct</code> takes pairs of arguments, where the first argument in the pair is the fieldname to include in the structure and the second is a scalar or cell array, representing the values to include in the structure or structure array. For example <pre class="example"> struct ("field1", 1, "field2", 2) ⇒ ans = { field1 = 1 field2 = 2 } </pre> <p>If the values passed to <code>struct</code> are a mix of scalar and cell arrays, then the scalar arguments are expanded to create a structure array with a consistent dimension. For example <pre class="example"> s = struct ("field1", {1, "one"}, "field2", {2, "two"}, "field3", 3); s.field1 ⇒ ans = 1 ans = one s.field2 ⇒ ans = 2 ans = two s.field3 ⇒ ans = 3 ans = 3 </pre> <p>If you want to create a struct which contains a cell array as an individual field, you have to put it into another cell array like in the following example: <pre class="example"> struct ("field1", {{1, "one"}}, "field2", 2) ⇒ ans = { field1 = { [1,1] = 1 [1,2] = one } field2 = 2 } </pre> <!-- ov-struct.cc --> <p><a name="doc_002dstruct"></a> <div class="defun"> — Built-in Function: <b>struct</b> (<var>"field", value, "field", value, <small class="dots">...</small></var>)<var><a name="index-struct-373"></a></var><br> <blockquote> <p>Create a structure and initialize its value. <p>If the values are cell arrays, create a structure array and initialize its values. The dimensions of each cell array of values must match. Singleton cells and non-cell values are repeated so that they fill the entire array. If the cells are empty, create an empty structure array with the specified field names. <p>If the argument is an object, return the underlying struct. </p></blockquote></div> <p>The function <code>isstruct</code> can be used to test if an object is a structure or a structure array. <!-- ov-struct.cc --> <p><a name="doc_002disstruct"></a> <div class="defun"> — Built-in Function: <b>isstruct</b> (<var>expr</var>)<var><a name="index-isstruct-374"></a></var><br> <blockquote><p>Return 1 if the value of the expression <var>expr</var> is a structure. </p></blockquote></div> </body></html>