Matlab Struct Array Assignment

Programming and Data Types   

Accessing Data in Structure Arrays

Using structure array indexing, you can access the value of any field or field element in a structure array. Likewise, you can assign a value to any field or field element. For the examples in this section, consider this structure array.

You can access subarrays by appending standard subscripts to a structure array name. For example, the line below results in a 1-by-2 structure array.

  • mypatients = patient(1:2) 1x2 struct array with fields: name billing test

The first structure in the array is the same as the first structure in the array.

  • mypatients(1) ans = name: 'John Doe' billing: 127 test: [3x3 double]

To access a field of a particular structure, include a period (.) after the structure name followed by the field name.

  • str = patient(2).name str = Ann Lane

To access elements within fields, append the appropriate indexing mechanism to the field name. That is, if the field contains an array, use array subscripting; if the field contains a cell array, use cell array subscripting, and so on.

  • test2b = patient(3).test(2,2) test2b = 153

Use the same notations to assign values to structure fields, for example,

  • patient(3).test(2,2) = 7;

You can extract field values for multiple structures at a time. For example, the line below creates a 1-by-3 vector containing all of the fields.

  • bills = [patient.billing] bills = 127.0000 28.5000 504.7000

Similarly, you can create a cell array containing the data for the first two structures.

  • tests = {patient(1:2).test} tests = [3x3 double] [3x3 double]

Accessing Data Using Dynamic Field Names

All of the structures discussed up to this point have elements that can only be referenced using fixed field names. Another means of accessing structures is to use dynamic field names. These names express the field as a variable expression that MATLAB evaluates at run-time. The dot-parentheses syntax shown here makes a dynamic field name.

Index into this field using the standard MATLAB indexing syntax. For example, to evaluate into a field name and obtain the values of that field at columns through of row , use

  • struct_name.(expression)(7,1:25)

Example.   The function computes an average test score, retrieving information from the structure using dynamic field names.

  • function avg = avgscore(testscores, student, first, last) for k = first:last scores(k) = testscores.(student).week(k); end avg = sum(scores)/(last - first + 1);

You can run this function using different values for the dynamic field, .

  • avgscore(testscores, 'Ann_Lane', 1, 20) ans = 83.5000 avgscore(testscores, 'William_King', 1, 20) ans = 92.1000

  Building Structure Arrays Finding the size of Structure Arrays 

Hello, I will use a simple example to show what I am after. I have a structure array 'person' with fields ('First_Name' , 'Last_Name' , 'FamilyInfo'), where 'FamilyInfo' is a structure with three fields ('FatherName' , 'MotherName' , 'NumberOfSiblings').

I create 4 elements in person structure array and fill all the 'First_Name' and 'Last_Name' fields but the 'FamilyInfo' field is left empty. Now, for all the 4 elements in the 'person' structure array, I want to assign at once the same value to the field "NumberOfsiblings" in the nested structure 'Family', but MATLAB throws an error. Here is the code;

Create an empty structre array

person = struct('First_Name',[],'Last_Name',[],... 'FamilyInfo',struct('FatherName',[],'MotherName',[],'NumberOfsiblings',[]));

Create elements in the array but the Family field is left empty

FirstNames = {'Joe','Jack','William','Averel'}; [person(1:4).First_Name]=deal(FirstNames{:}); [person(1:4).Last_Name]=deal('Dalton');

For all the elements in person structure array, assign the same value (3) to the NumberOfsiblings field, of the Family field.

[person(1:4).FamilyInfo.NumerOfsiblings] = 3;

Error message pops up;

Expected one output from a curly brace or dot indexing expression, but there were 4 results.

Is there a way to do it without using a for loop? One-line code is preferred. Thanks


Leave a Reply

Your email address will not be published. Required fields are marked *