1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
<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.11">
<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">
<p>
<a name="Creating-Structures"></a>
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>
|