Structure Arrays Matlab Help

2.7 Structure Arrays

Structure arrays are composed of structures. This class of arrays enables you to store dissimilar arrays together. The elements in structures are accessed using . named fields. This feature: distinguishes them from cell arrays, which are accssed using the standard array indexing operations

Arrangementof data in the structure array student.

Arrangementof data in the structure array student.

Structure arrays are used in this text only in (his section and in Chapter 10. Some MATLAB toolboxes do use structure arrays. A specific example is the best way to introduce the terminology of structures. Suppose you want to create a database of students in a course, and you want to include each student’s name, Social Security number, email address, and test scores. Figure 2.7-1 shows a diagram of this data structure. Each type of data
(name, Social Security number, and so on) is afield, and its name is the field name. Thus our database has four fields. The first three fields each contain a text string, while the last field (the test scores) contains a vector having numerical elements. A structure consists of all this information for a single student. A structure array is
an array of such structures for different students. The array shown in Figure 2.7-1 has two structures arranged in one row and two columns

Creating Structures
You can create a structure array by using assignment statements or by using the struct function. The following example uses assignment statements to build a structure. Structure arrays use the dot notation (.) to specify and to access the fields. You can type the commands either in the interactive mode or in a script file.

You can also obtain information about the fields by using the field names function (see Table 2.7-1).

For example:
»field names(student)
ans =
‘name’
‘SSN’
’email’
‘tests’
As you.fill in more student information, MATLAB assigns the same number of fields and the same field names to each element. If you do not enter some information-for example, suppose you do not know someone’s email address-MATLAB assigns an empty matrix to that field for that student. The fields, can have different sizes. For example, each name field can contain a different number of characters, and the arrays containing the test scores can be different sizes, as would be the case if a certain student did not take the second test.

In addition to the assignment statement, you can also build structures using “the structure function, which lets you “preallocate” a structure array. To build a structure array named sa-1, the syntax is sa-1 = struct (‘field1 , ‘values1, ,field 2’ , ‘values 2’,) where the arguments are the field names and their values. The values arrays
values 1, values 2,  must all be arrays of the same size, scalar cells, or single values. The elements of the values arrays are inserted into the corresponding elements of the structure array. The resulting structure array has the same size as the values arrays, or is I x 1 if none of the values arrays is a cell. For example, to preallocate a 1 x 1 structure array for the student database, you type student = struct(‘name’, ‘John Smith’, ‘SSN 392-77-1786’, .email ••• smithj@myschool.edu

‘tests’, [67,75,84]

Accessing Structure Arrays
To access the contents of a particular field, type a period after the structure array name, followed by the field name. For example, typing student (2) .name displays the value ‘Mary Jones’. Of course, we can assign the result to a variable in the usual way. For example, typing name2 = student (2).name assigns the value’ Mary Jones’ to the variable name2. To access elements within a field, for example; John Smith’s second test score, type student (1) . tests (2) .This entry returns the value 7 5. In general, if a field contains an array, you use the array’s subscripts to access its elements. In this example the statement student (1) . tests (2) is equivalent to student (1,1) . tests (2) because student has one row.

To store all the information for a particular structure-say, all the information
about Mary Jones-in another structure array named M, you type M = student (2).

 You can also assign or change values of field elements. For example, typing student (2) . tests (2) = 81 changes Mary Jones’s second test score from 78 to 81. Direct indexing is usually the best way to create or access field values. However, suppose you used the field names function in an M-file to obtain a field name. You would know the field name only as a string. In this situation you can use the set field and ‘get field functions (or assigning and retrieving field values. For example, typing setfield (M, ‘name’, ‘Mary Lee Jones’) inserts the new name. Typing get field (M, ‘name’ ) returns the result ans =Mary Lee Jones.

 The preceding syntax for the getfield and setfield functions works on 1 x 1 arrays only. The alternate syntax, which works for an i x j array s, is

F = getfield(S, {i,j}’ ‘field’, (k})

 which is equivalent to F = S (i, j) . field (k). For example,

getfield(student, {1,1}, ‘tests’, {2})”, \

returns the result ans = 75. Similarly,

S = setfield(S, {i,j}, ‘field’, (k}) \

 is equivalent to S (i, j) . field (k ) = s.

Modifying Structures
Suppose you want to add phone numbers to the database.You can do this by typing the first student’s phone number as follows:

 student(l) .phone = ‘555-1653’

All the other structures in the array will now have a phone field, but these fields will contain the empty array until you give them values.

 To delete a field from every structure in the array, use the rmfield function. Its basic syntax is

 new structure = rmfield(array, ‘field’);

 where array is the structure array to be modified, ‘field’ is the field to be removed, and new_structure is the name of the new structure array so created by the removal of the field. For example, to remove the Social Security field and call the new structure array new_student, type

 new_student = rmfield(student, ‘SSN’);

Using Operators and Functions with Structures
You can apply the MATLAB operators to structures in the usual way. For example, to find the maximum test score of the second student, you type max (student (2) . tests). The answer is 93.

 The is field function determines whether or not a structure array contains a particular field. Its syntax is isfield (S, ‘field’). It returns a value of 1 (which means “true”) if ‘f i e 1d’ is the name of a field in the structure array S. For example, typing is field (student, ‘name’) returns the result ans = 1.

 The isstruct function determines whether or not an array is a structure
array. Its syntax is is s true t (S ) . It returns a value of 1 if s is a structure array, and 0 otherwise. For example, typing is s true t (student) returns the result ans = 1, which is equivalent to “true.”

Dynamic Field Names and New Structure Syntax
Prior to MATLAB 6.5, the elements of a structure could be referenced by using fixed field names only. As of MATLAB 6.5 you can reference structures using field names that express the field as a variable expression that is evaluated at run time.

 Structures can now be referenced by using dynamic field names that express the fields as variable expressions that are computed at run time. Use the following parentheses syntax to specify which fields are to be dynamic:

 structure_name. (expression)

where expression is the dynamic field name. Use the standard indexing method to access the information in the structure. For example, typing

struct_name. (expression) (5,1:10)
accesses the information in the field in row 5, columns 1 through 10.
The foIlowing table compares the various static and dynamic data types.

Capture

Because of the new dynamic names feature, set field and getfield are less useful than before. The setfield and get field functions execute more slowly because they are not built-in MATLAB functions. In addition to increased execution speed, dynamic field names offer improved readability over setfield and getfieid. For example, consider the.
following:
1. Using setfield:
S = setfield(S, {m,n},fieldname, {k},value)
2. Using a dynamic field name:
S(m,n). (fieldname) (k) value

Test Your Understanding’
T2.7-1 Create the structure array student shown in Figure 2.7-1 and add the following information about a third student: name: Alfred E. Newman; SSN: 555-12-3456; e-mail: newmana@myschool.edu; tests: 55,45,58.
T2.7-2 Edit your structure array to change Mr. Newman’s second test score from
45 to 53.
T2.7-3 Edit your structure array to remove the SSN field.

Posted on July 25, 2015 in Numeric Cell And Structure Arrays

Share the Story

Back to Top
Share This