• MATLAB Answers
  • File Exchange
  • AI Chat Playground
  • Discussions
  • Communities
  • Treasure Hunt
  • Community Advisors
  • Virtual Badges
  • Trial software

You are now following this question

  • You will see updates in your followed content feed .
  • You may receive emails, depending on your communication preferences .

Assign value to field of nested structure in a structure array

Konstantinos Tompoulidis

Direct link to this question

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array

   2 Comments Show None Hide None

Pico Technology

Direct link to this comment

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399200

Konstantinos Tompoulidis

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399217

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Guillaume

Direct link to this answer

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#answer_239348

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399266

Guillaume

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399267

More Answers (2)

Jan

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#answer_239344

   4 Comments Show 2 older comments Hide 2 older comments

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399219

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399277

Jan

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399934

broken_arrow

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_1761244

Jos (10584)

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#answer_239372

   1 Comment Show -1 older comments Hide -1 older comments

https://www.matlab.com/matlabcentral/answers/307525-assign-value-to-field-of-nested-structure-in-a-structure-array#comment_399275

  • nested structure
  • assign value to field of all elements
  • structure array

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 简体中文 Chinese
  • 日本 Japanese (日本語)
  • 한국 Korean (한국어)

Contact your local office

  • MATLAB Answers
  • File Exchange
  • AI Chat Playground
  • Discussions
  • Communities
  • Treasure Hunt
  • Community Advisors
  • Virtual Badges
  • Trial software

You are now following this question

  • You will see updates in your followed content feed .
  • You may receive emails, depending on your communication preferences .

Assigning an Array to Structure Field

Nick G

Direct link to this question

https://in.mathworks.com/matlabcentral/answers/238820-assigning-an-array-to-structure-field

   0 Comments Show -2 older comments Hide -2 older comments

Sign in to comment.

Sign in to answer this question.

Answers (1)

Walter Roberson

Direct link to this answer

https://in.mathworks.com/matlabcentral/answers/238820-assigning-an-array-to-structure-field#answer_191005

  • structure field

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 简体中文 Chinese
  • 日本 Japanese (日本語)
  • 한국 Korean (한국어)

Contact your local office

MATLAB Programming/Arrays/Struct Arrays

  • 1 Introduction to Structures
  • 2 Declaring Structures
  • 3 Arrays of Structures
  • 4 Accessing Fields
  • 5 Accessing Array Elements
  • 6 Assigning values to a field of each struct array element
  • 7 Sub-arrays through logical addressing

Introduction to Structures [ edit | edit source ]

MATLAB provides a means for structure data elements. Structures are created and accessed in a manner familiar for those accustomed to programming in C.

MATLAB has multiple ways of defining and accessing structure fields. See Declaring Structures for more details.

Note: Structure field names must begin with a letter, and are case-sensitive. The rest of the name may contain letters, numerals, and underscore characters. Use the namelengthmax function to determine the maximum length of a field name.

Declaring Structures [ edit | edit source ]

Structures can be declared using the struct command.

In MATLAB, variables do not require explicit declaration before their use. As a result structures can be declared with the '.' operator.

Structures can be declared as needed and so can the fields.

Arrays of Structures [ edit | edit source ]

Structures can also be arrays. Below is an example

Accessing Fields [ edit | edit source ]

When the field name is known the field value can be accessed directly.

In some cases you may need to access the field dynamically which can be done as follows.

Accessing Array Elements [ edit | edit source ]

Any given element in a structure array can be accessed through an array index like this

To access all elements in a structure array use the syntax {structure.field}. In order to get all values in a vector or array use square brackets ([]) as seen below.

Or you can put them all into a cell array (rather than concatenating them) like this:

Assigning values to a field of each struct array element [ edit | edit source ]

Matlab provides tools to assign values to a field of each array element. Consider the following struct array:

The following command assigns the same value to the field_b field of each array element:

To assign different values to each array element:

Sub-arrays through logical addressing [ edit | edit source ]

With Matlab, it's possible to extract a subarray from an array by using logical indexing. Consider the following struct array:

To obtain a subarray from foo where all foo.field_a values are equal to 2, a boolean array can be used to perform logical indexing. So, a boolean test that returns a boolean array for this purpose would be:

So, by using this boolean array to perform logical indexing, Matlab defines a struct array whose elements consist of those from foo whose field_a value is equal to 2 by doing:

matlab assign array to struct field

  • Book:MATLAB Programming

Navigation menu

Help Center Help Center

  • Hilfe-Center
  • Produkt-Updates
  • Documentation

Assign value to structure array field

Description

S = setfield( S , field , value ) assigns a value to the specified field of the structure S . For example, S = setfield(S,'a',1) makes the assignment S.a = 1 .

As an alternative to setfield , use dot notation: S.field = value . Dot notation is typically more efficient.

If S does not have the specified field, then setfield creates it and assigns value to it.

S = setfield( S , field 1,..., field N, value ) assigns a value to the specified field of a nested structure. For example, S = setfield(S,'a','b','c',1) makes the assignment S.a.b.c = 1 , where the fields S.a and S.a.b are also structures.

S = setfield( S , idx , field 1,..., field N, value ) specifies an element of S and assigns a value to one of its fields. For example, S = setfield(S,{3,4},'a',1) makes the assignment S(3,4).a = 1 .

S = setfield( S , idx , field 1, idx 1,..., field N, idx N, value ) specifies elements of fields. For example, S = setfield(S,'a',{2},1) makes the assignment S.a(2) = 1 . Similarly, S = setfield(S,{3,4},'a',{2},'b',1) makes the assignment S(3,4).a(2).b = 1 .

collapse all

Assign Values to Fields

Create a scalar structure.

Assign a value to a field using the setfield function.

Assign a value to another field. If you specify a field that does not exist, then setfield creates it.

You also can assign a value to a field using dot notation.

Field of Nested Structure

Create a nested structure. In a nested structure, a structure at any level can have fields that are structures, and other fields that are not structures.

While S is a structure, the fields S.a , S.a.b , and S.a.b.e are also structures.

Assign a value to S.a.b.d using the setfield function. When you specify a comma-separated list of nested structure names, include the structure names at every level between the top and the field name you specify. In this case, the comma-separated list of structure names is 'a','b' and the field name is 'd' .

You also can use dot notation to assign a value.

Fields of Elements of Structure Array

Assign values to fields of elements of a structure array.

First, create a structure array. As in all structure arrays, each element is a structure with the same fields.

You also can assign values using setfield . If a field does not exist, setfield creates it. Create a field named title .

The setfield function assigns a value to a field of an individual element, but the output argument is the entire structure array.

Display the first element of S .

As an alternative, index into the structure array, and then use dot notation to assign a value to a field of an element.

Indices of Nested Structure Array

Assign a value to a field of a nested structure, in which the structures at some levels are structure arrays. In this example, S is a 1-by-2 structure array. The second element, S(2) , has a nested structure a.b , where b is a 1-by-3 structure array.

First, create a nested structure. After creating the structure using dot notation, create another nonscalar structure array using the struct function and add it as a field.

Display the third element of S(2).a.b .

Assign a new value to the field d of S(2).a.b(3) using the setfield function. Display the structure with the updated field.

Elements of Field

Create a structure with a field whose value is an array.

Assign values to elements of S.a using the setfield function. To assign values to particular elements, specify indices after the name of the field. You must specify the indices within a cell array. However, specify the new values in an array whose data type matches the data type of the field.

You also can use dot notation and array indexing to assign values to the same elements.

Input Arguments

S — structure array structure array.

Structure array. If S is nonscalar, then each element of S is a structure, and all elements have the same fields with the same names.

field — Field name character vector | string scalar

Field name, specified as a character vector or string scalar.

idx — Indices cell array of numeric or logical values

Indices, specified as a cell array of numeric or logical values. Indices for S and fields 1 through N-1 specify individual elements of structure arrays. Indices for field N specify one or more elements of the array in that field, which can be of any type.

Example: S = setfield(S,{1,2},'a',1) is equivalent to S(1,2).a = 1 .

Example: If S.a = [5 10 20] , then S = setfield(S,'a',{[2,3]},[50 100]) is equivalent to S.a(2:3) = [50 100] .

value — Values array

Values, specified as any type of array having any size.

Extended Capabilities

C/c++ code generation generate c and c++ code using matlab® coder™..

Usage notes and limitations:

Field name must be constant.

Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment .

Version History

Introduced before R2006a

getfield | fieldnames | isfield | orderfields | rmfield | struct

  • Structure Arrays
  • Generate Field Names from Variables

MATLAB-Befehl

Sie haben auf einen Link geklickt, der diesem MATLAB-Befehl entspricht:

Führen Sie den Befehl durch Eingabe in das MATLAB-Befehlsfenster aus. Webbrowser unterstützen keine MATLAB-Befehle.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

  • MATLAB Answers
  • File Exchange
  • AI Chat Playground
  • Discussions
  • Communities
  • Treasure Hunt
  • Community Advisors
  • Virtual Badges
  • Trial software

You are now following this question

  • You will see updates in your followed content feed .
  • You may receive emails, depending on your communication preferences .

getfield to get values from an entire struct array?

Ken

Direct link to this question

https://matlabcentral.mathworks.com/matlabcentral/answers/2100666-getfield-to-get-values-from-an-entire-struct-array

   1 Comment Show -1 older comments Hide -1 older comments

Stephen23

Direct link to this comment

https://matlabcentral.mathworks.com/matlabcentral/answers/2100666-getfield-to-get-values-from-an-entire-struct-array#comment_3114906

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Bruno Luong

Direct link to this answer

https://matlabcentral.mathworks.com/matlabcentral/answers/2100666-getfield-to-get-values-from-an-entire-struct-array#answer_1433506

   3 Comments Show 1 older comment Hide 1 older comment

Ken

https://matlabcentral.mathworks.com/matlabcentral/answers/2100666-getfield-to-get-values-from-an-entire-struct-array#comment_3114916

Bruno Luong

https://matlabcentral.mathworks.com/matlabcentral/answers/2100666-getfield-to-get-values-from-an-entire-struct-array#comment_3114926

https://matlabcentral.mathworks.com/matlabcentral/answers/2100666-getfield-to-get-values-from-an-entire-struct-array#comment_3115386

More Answers (0)

  • struct array

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 简体中文 Chinese
  • 日本 Japanese (日本語)
  • 한국 Korean (한국어)

Contact your local office

Help Center Help Center

  • Help Center
  • Trial Software
  • Product Updates
  • Documentation

Structure array

Description

A structure array is a data type that groups related data using data containers called fields . Each field can contain any type of data. Access data in a field using dot notation of the form structName.fieldName .

When you have data to put into a new structure, create the structure using dot notation to name its fields one at a time:

Field names can contain ASCII letters (A–Z, a–z), digits (0–9), and underscores, and must begin with a letter. The maximum length of a field name is namelengthmax .

You also can create a structure array using the struct function, described below. You can specify many fields simultaneously, or create a nonscalar structure array.

s = struct creates a scalar (1-by-1) structure with no fields.

s = struct( field , value ) creates a structure array with the specified field and value. The value input argument can be any data type, such as a numeric, logical, character, or cell array.

If value is not a cell array, or if value is a scalar cell array, then s is a scalar structure. For instance, s = struct('a',[1 2 3]) creates a 1-by-1 structure, where s.a = [1 2 3] .

If value is a nonscalar cell array, then s is a structure array with the same dimensions as value . Each element of s contains the corresponding element of value . For example, s = struct('x',{'a','b'}) returns s(1).x = 'a' and s(2).x = 'b' .

If value is an empty cell array {} , then s is an empty (0-by-0) structure.

s = struct(field1,value1,...,fieldN,valueN) creates a structure array with multiple fields.

If none of the value inputs are cell arrays, or if all value inputs that are cell arrays are scalars, then s is a scalar structure.

If any of the value inputs is a nonscalar cell array, then s has the same dimensions as that cell array. Also, if two or more value inputs are nonscalar cell arrays, then they all must have the same dimensions.

For any value that is a scalar cell array or an array of any other data type, struct inserts the contents of value in the relevant field for all elements of s . For example, s = struct('x',{'a','b'},'y','c') returns s(1).x = 'a' , s(2).x = 'b' , s(1).y = 'c' , and s(2).y = 'c' .

If any value input is an empty cell array, {} , then output s is an empty (0-by-0) structure. To specify an empty field and keep the values of the other fields, use [] as a value input instead.

s = struct([]) creates an empty (0-by-0) structure with no fields.

s = struct( obj ) creates a scalar structure with field names and values that correspond to properties of obj . The struct function does not convert obj , but rather creates s as a new structure. This structure does not retain the class information, so private, protected, and hidden properties become public fields in s . The struct function issues a warning when you use this syntax.

Input Arguments

Field — field name character vector | string scalar.

Field name, specified as a character vector or string scalar.

value — Values array

Values, specified as any type of array. If any value input is a nonscalar cell array, then all nonscalar cell array inputs must have the same dimensions.

If any value input is an empty cell array, {} , then the output is an empty structure array. To specify a single empty field, use [] .

obj — Object object

The struct function copies the properties of obj to the fields of a new scalar structure.

The struct function does not create a structure from most of the fundamental data types. For example, if obj has the double or char data type, then struct issues an error message. However, struct does return the properties of a table or timetable as a structure. See Fundamental MATLAB Classes for the list of fundamental data types.

collapse all

Store Related Data Variables in Structure

Store related pieces of data in the fields of a structure. You can give the fields human-readable names that describe the data.

Create a structure by adding fields to it using dot notation. The fields contain x- and y-values for a sine wave, and text that describes the data.

Plot the sine wave. You can refer to the arrays of x- and y-values by their field names. Then add the title.

matlab assign array to struct field

Structure with One Field

Create a nonscalar structure that contains a single field.

View the contents of each element.

When you access a field of a nonscalar structure, such as s.f , MATLAB® returns a comma-separated list. In this case, s.f is equivalent to s(1).f, s(2).f, s(3).f .

Structure with Multiple Fields

Create a nonscalar structure that contains several fields.

The cell arrays for value2 and value3 are 1-by-2, so s is also 1-by-2. Because value1 is a numeric array and not a cell array, both s(1).f1 and s(2).f1 have the same contents. Similarly, because the cell array for value4 has a single element, s(1).f4 and s(2).f4 have the same contents.

Structure with Empty Field

Create a structure that contains an empty field. Use [] to specify the value of the empty field.

Fields with Cell Arrays

Create a structure with a field that contains a cell array.

Empty Structure

Create an empty structure that contains several fields.

Assign a value to a field in an empty structure.

Nested Structure

Create a nested structure, where a is a structure with a field that contains another structure.

View the names of the fields of a.b .

Extended Capabilities

C/c++ code generation generate c and c++ code using matlab® coder™..

Usage notes and limitations:

If the value argument is a cell array, all elements must have the same type.

Struct array field names must have the same length. For example, this code errors because 'my_int' and 'my_double' are different lengths: param1.name = 'my_int' ; param1.value = int32(1); param2.name = 'my_double' ; param2.value = 2.0; params = [param1, param2];

Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment .

Version History

R2023b: partial contents as well as size and data type of arrays in structure fields are displayed.

Between R2021b and R2023b, MATLAB ® displayed the entire contents of an array in a structure field if the contents fit in the display. If the array did not fit, MATLAB showed as much of the array as the display allowed.

Starting in R2023b, if the entire contents of an array in a field do not fit in the display, MATLAB shows as much of the array as the display allows, as well as the size and data type of the array. For example, as of R2023b, MATLAB displays partial contents as well as the size and data type for two fields that contain arrays of 100 double values.

If the first element of the array is itself too large to display, such as an enumeration member with a long name, MATLAB displays only the size and data type of the array.

The disp function follows the same behavior.

R2021b: Partial contents of arrays in structure fields are displayed instead of size and data type

Before R2021b, MATLAB displayed the entire contents of an array in a structure field if the contents fit in the display. If the array did not fit, MATLAB displayed the size and data type of the array.

Starting in R2021b, if the entire contents of an array do not fit in the display, MATLAB shows as much of the array as the display allows instead of the size and data type. For example, as of R2021b, MATLAB displays partial contents of two structure fields that contain arrays of 100 double values.

R2019a: Structure array expansion is consistent with general array expansion

Starting in R2019a, the dimensions of an expanded structure array are consistent whether you assign a value to a single field using dot notation or assign an entire structure to the array. Previously, the output dimensions were different when you did not specify indices for all dimensions. Assigning to a field using dot notation now matches the previous behavior of assigning a structure, which is consistent with general array expansion. For example:

If S is 1-by-2-by-3-by-4, then S(5,2)=struct('a',3) and S(5,2).a=3 both expand S to 5-by-2-by-3-by-4. Previously, S(5,2).a=3 resulted in a 5-by-24 array.

If S is 0-by-5 or 5-by-0, then S(3)=struct('a',2) and S(3).a=2 both expand S to 1-by-3. Previously, S(3).a=1 resulted in a 1-by-5 array when S was 0-by-5 and in a 3-by-1 array when S was 5-by-0.

If S is 0-by-2-by-3, then S(3) = struct('a',2) and S(3).a=2 produce errors because it is ambiguous which dimension to expand. Previously, S(3).a=2 resulted in a 1-by-6 array.

If S is 1-by-1-by-3 with two fields, then S(5)=struct('a',7,'b',[]) and S(5).a=7 both expand S to 1-by-1-by-5. Previously, S(5).a=7 resulted in a 1-by-5 array.

If S is 0-by-1 with two fields, then S(5)=struct('a',7,'b',[]) and S(5).a=7 both expand S to 5-by-1. Previously, S(5).a=7 resulted in a 1-by-5 array.

isfield | table | isstruct | fieldnames | orderfields | rmfield | substruct | cell2struct | struct2cell | struct2table | table2struct | namelengthmax

  • Structure Arrays
  • Access Data in Nested Structures
  • Generate Field Names from Variables

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

  • MATLAB Answers
  • File Exchange
  • AI Chat Playground
  • Discussions
  • Communities
  • Treasure Hunt
  • Community Advisors
  • Virtual Badges
  • Trial software

You are now following this question

  • You will see updates in your followed content feed .
  • You may receive emails, depending on your communication preferences .

How to assign a struct array to a field of another struct array?

Juan Sierra

Direct link to this question

https://www.mathworks.com/matlabcentral/answers/446352-how-to-assign-a-struct-array-to-a-field-of-another-struct-array

   2 Comments Show None Hide None

madhan ravi

Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/446352-how-to-assign-a-struct-array-to-a-field-of-another-struct-array#comment_673933

Juan Sierra

https://www.mathworks.com/matlabcentral/answers/446352-how-to-assign-a-struct-array-to-a-field-of-another-struct-array#comment_673935

  • testData.mat

Sign in to comment.

Sign in to answer this question.

Accepted Answer

Stephen23

Direct link to this answer

https://www.mathworks.com/matlabcentral/answers/446352-how-to-assign-a-struct-array-to-a-field-of-another-struct-array#answer_362191

  • from a cell array, or
  • from the field of a structure (we can use this on the LHS)

https://www.mathworks.com/matlabcentral/answers/446352-how-to-assign-a-struct-array-to-a-field-of-another-struct-array#comment_673936

Stephen23

https://www.mathworks.com/matlabcentral/answers/446352-how-to-assign-a-struct-array-to-a-field-of-another-struct-array#comment_673942

More Answers (0)

  • struct-array

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

An Error Occurred

Unable to complete the action because of changes made to the page. Reload the page to see its updated state.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 简体中文 Chinese
  • 日本 Japanese (日本語)
  • 한국 Korean (한국어)

Contact your local office

Help Center Help Center

  • Help Center
  • Trial Software
  • Product Updates
  • Documentation

Structure array

Description

A structure array is a data type that groups related data using data containers called fields . Each field can contain any type of data. Access data in a field using dot notation of the form structName.fieldName .

When you have data to put into a new structure, create the structure using dot notation to name its fields one at a time:

Field names can contain ASCII letters (A–Z, a–z), digits (0–9), and underscores, and must begin with a letter. The maximum length of a field name is namelengthmax .

You also can create a structure array using the struct function, described below. You can specify many fields simultaneously, or create a nonscalar structure array.

s = struct creates a scalar (1-by-1) structure with no fields.

s = struct( field , value ) creates a structure array with the specified field and value. The value input argument can be any data type, such as a numeric, logical, character, or cell array.

If value is not a cell array, or if value is a scalar cell array, then s is a scalar structure. For instance, s = struct('a',[1 2 3]) creates a 1-by-1 structure, where s.a = [1 2 3] .

If value is a nonscalar cell array, then s is a structure array with the same dimensions as value . Each element of s contains the corresponding element of value . For example, s = struct('x',{'a','b'}) returns s(1).x = 'a' and s(2).x = 'b' .

If value is an empty cell array {} , then s is an empty (0-by-0) structure.

s = struct(field1,value1,...,fieldN,valueN) creates a structure array with multiple fields.

If none of the value inputs are cell arrays, or if all value inputs that are cell arrays are scalars, then s is a scalar structure.

If any of the value inputs is a nonscalar cell array, then s has the same dimensions as that cell array. Also, if two or more value inputs are nonscalar cell arrays, then they all must have the same dimensions.

For any value that is a scalar cell array or an array of any other data type, struct inserts the contents of value in the relevant field for all elements of s . For example, s = struct('x',{'a','b'},'y','c') returns s(1).x = 'a' , s(2).x = 'b' , s(1).y = 'c' , and s(2).y = 'c' .

If any value input is an empty cell array, {} , then output s is an empty (0-by-0) structure. To specify an empty field and keep the values of the other fields, use [] as a value input instead.

s = struct([]) creates an empty (0-by-0) structure with no fields.

s = struct( obj ) creates a scalar structure with field names and values that correspond to properties of obj . The struct function does not convert obj , but rather creates s as a new structure. This structure does not retain the class information, so private, protected, and hidden properties become public fields in s . The struct function issues a warning when you use this syntax.

Input Arguments

Field — field name character vector | string scalar.

Field name, specified as a character vector or string scalar.

value — Values array

Values, specified as any type of array. If any value input is a nonscalar cell array, then all nonscalar cell array inputs must have the same dimensions.

If any value input is an empty cell array, {} , then the output is an empty structure array. To specify a single empty field, use [] .

obj — Object object

The struct function copies the properties of obj to the fields of a new scalar structure.

The struct function does not create a structure from most of the fundamental data types. For example, if obj has the double or char data type, then struct issues an error message. However, struct does return the properties of a table or timetable as a structure. See Fundamental MATLAB Classes for the list of fundamental data types.

collapse all

Store Related Data Variables in Structure

Store related pieces of data in the fields of a structure. You can give the fields human-readable names that describe the data.

Create a structure by adding fields to it using dot notation. The fields contain x- and y-values for a sine wave, and text that describes the data.

Plot the sine wave. You can refer to the arrays of x- and y-values by their field names. Then add the title.

matlab assign array to struct field

Structure with One Field

Create a nonscalar structure that contains a single field.

View the contents of each element.

When you access a field of a nonscalar structure, such as s.f , MATLAB® returns a comma-separated list. In this case, s.f is equivalent to s(1).f, s(2).f, s(3).f .

Structure with Multiple Fields

Create a nonscalar structure that contains several fields.

The cell arrays for value2 and value3 are 1-by-2, so s is also 1-by-2. Because value1 is a numeric array and not a cell array, both s(1).f1 and s(2).f1 have the same contents. Similarly, because the cell array for value4 has a single element, s(1).f4 and s(2).f4 have the same contents.

Structure with Empty Field

Create a structure that contains an empty field. Use [] to specify the value of the empty field.

Fields with Cell Arrays

Create a structure with a field that contains a cell array.

Empty Structure

Create an empty structure that contains several fields.

Assign a value to a field in an empty structure.

Nested Structure

Create a nested structure, where a is a structure with a field that contains another structure.

View the names of the fields of a.b .

Extended Capabilities

C/c++ code generation generate c and c++ code using matlab® coder™..

Usage notes and limitations:

If the value argument is a cell array, all elements must have the same type.

Struct array field names must have the same length. For example, this code errors because 'my_int' and 'my_double' are different lengths: param1.name = 'my_int' ; param1.value = int32(1); param2.name = 'my_double' ; param2.value = 2.0; params = [param1, param2];

Thread-Based Environment Run code in the background using MATLAB® backgroundPool or accelerate code with Parallel Computing Toolbox™ ThreadPool .

This function fully supports thread-based environments. For more information, see Run MATLAB Functions in Thread-Based Environment .

Version History

R2023b: partial contents as well as size and data type of arrays in structure fields are displayed.

Between R2021b and R2023b, MATLAB ® displayed the entire contents of an array in a structure field if the contents fit in the display. If the array did not fit, MATLAB showed as much of the array as the display allowed.

Starting in R2023b, if the entire contents of an array in a field do not fit in the display, MATLAB shows as much of the array as the display allows, as well as the size and data type of the array. For example, as of R2023b, MATLAB displays partial contents as well as the size and data type for two fields that contain arrays of 100 double values.

If the first element of the array is itself too large to display, such as an enumeration member with a long name, MATLAB displays only the size and data type of the array.

The disp function follows the same behavior.

R2021b: Partial contents of arrays in structure fields are displayed instead of size and data type

Before R2021b, MATLAB displayed the entire contents of an array in a structure field if the contents fit in the display. If the array did not fit, MATLAB displayed the size and data type of the array.

Starting in R2021b, if the entire contents of an array do not fit in the display, MATLAB shows as much of the array as the display allows instead of the size and data type. For example, as of R2021b, MATLAB displays partial contents of two structure fields that contain arrays of 100 double values.

R2019a: Structure array expansion is consistent with general array expansion

Starting in R2019a, the dimensions of an expanded structure array are consistent whether you assign a value to a single field using dot notation or assign an entire structure to the array. Previously, the output dimensions were different when you did not specify indices for all dimensions. Assigning to a field using dot notation now matches the previous behavior of assigning a structure, which is consistent with general array expansion. For example:

If S is 1-by-2-by-3-by-4, then S(5,2)=struct('a',3) and S(5,2).a=3 both expand S to 5-by-2-by-3-by-4. Previously, S(5,2).a=3 resulted in a 5-by-24 array.

If S is 0-by-5 or 5-by-0, then S(3)=struct('a',2) and S(3).a=2 both expand S to 1-by-3. Previously, S(3).a=1 resulted in a 1-by-5 array when S was 0-by-5 and in a 3-by-1 array when S was 5-by-0.

If S is 0-by-2-by-3, then S(3) = struct('a',2) and S(3).a=2 produce errors because it is ambiguous which dimension to expand. Previously, S(3).a=2 resulted in a 1-by-6 array.

If S is 1-by-1-by-3 with two fields, then S(5)=struct('a',7,'b',[]) and S(5).a=7 both expand S to 1-by-1-by-5. Previously, S(5).a=7 resulted in a 1-by-5 array.

If S is 0-by-1 with two fields, then S(5)=struct('a',7,'b',[]) and S(5).a=7 both expand S to 5-by-1. Previously, S(5).a=7 resulted in a 1-by-5 array.

isfield | table | isstruct | fieldnames | orderfields | rmfield | substruct | cell2struct | struct2cell | struct2table | table2struct | namelengthmax

  • Structure Arrays
  • Access Data in Nested Structures
  • Generate Field Names from Variables

MATLAB Command

You clicked a link that corresponds to this MATLAB command:

Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

  • Switzerland (English)
  • Switzerland (Deutsch)
  • Switzerland (Français)
  • 中国 (English)

You can also select a web site from the following list:

How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

  • América Latina (Español)
  • Canada (English)
  • United States (English)
  • Belgium (English)
  • Denmark (English)
  • Deutschland (Deutsch)
  • España (Español)
  • Finland (English)
  • France (Français)
  • Ireland (English)
  • Italia (Italiano)
  • Luxembourg (English)
  • Netherlands (English)
  • Norway (English)
  • Österreich (Deutsch)
  • Portugal (English)
  • Sweden (English)
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)

Contact your local office

IMAGES

  1. Cell and Struct Arrays in Matlab: Part 5 (of 16)

    matlab assign array to struct field

  2. GitHub

    matlab assign array to struct field

  3. Using Structures and Cell Arrays Video

    matlab assign array to struct field

  4. Cell and Struct Arrays in Matlab: Part 1 (of 16)

    matlab assign array to struct field

  5. Cell and Struct Arrays in Matlab: Part 2 (of 16)

    matlab assign array to struct field

  6. Creating structure arrays in MATLAB

    matlab assign array to struct field

VIDEO

  1. 78.How to use Struct Field in Databricks using PySpark in Telugu

  2. Program Data Mahasiswa menggunakan Struct Array pada C

  3. How to Resize an Image in Matlab ??

  4. MATLAB Image Processing: Eye detection CODE 2022

  5. Python Merge Dictionaries

  6. stream archive: creating article for Axum Headers (2023-11-09)

COMMENTS

  1. Assign value to structure array field

    To assign values to particular elements, specify indices after the name of the field. You must specify the indices within a cell array. However, specify the new values in an array whose data type matches the data type of the field. S = setfield(S, 'a' ,{3:5},[0 -50 -100]) S = struct with fields:

  2. How to assign values to a struct array with fields?

    Is there any way to write down a one-line script for assigning values to a struct array with fields? x(1).a=1; x(2).a=2; I'd like to change each value to 10 and 20 respectively. (I mean x(1)...

  3. Adding a new field to a struct array

    How do I go about adding the a field A.address to the array struct with the field name being define din the newstruct variable? The existing array should then look like the following: A(1).name = 'bob' ;

  4. assigning values to a field of an structure array in MATLAB

    Credits go to @Slayton, but you actually can do the same thing for assigning values too, using deal: [a([a.b]==1).b]=deal(3) So breakdown: [a.b] retrieves all b fields of the array a and puts this comma-separated-list in an array. a([a.b]==1) uses logical indexing to index only the elements of a that satisfy the constraint.

  5. Structure array

    Between R2021b and R2023b, MATLAB ® displayed the entire contents of an array in a structure field if the contents fit in the display. If the array did not fit, MATLAB showed as much of the array as the display allowed. Starting in R2023b, if the entire contents of an array in a field do not fit in the display, MATLAB shows as much of the array as the display allows, as well as the size and ...

  6. Assign value to field of nested structure in a structure array

    More importantly, note that when you grow the root source structure (by adding the first names of the 4 persons, only the 1st element of the structure array is a FamilyInfo substructure. The other 3 are initialised empty, because matlab does not copy the type of fields when it grows structure arrays.

  7. Assigning an Array to Structure Field

    Assigning an Array to Structure Field. Learn more about array, structure field

  8. MATLAB Programming/Arrays/Struct Arrays

    Assigning values to a field of each struct array element [edit | edit source] Matlab provides tools to assign values to a field of each array element. Consider the following struct array: foo = struct ('field_a', ... Matlab defines a struct array whose elements consist of those from foo whose field_a value is equal to 2 by doing: foo([foo.field ...

  9. Assigning an Array to Structure Field

    but your shuff is only a single output. If you want to assign the same value to each of the locations, use. Theme. Copy. [unshuff.trial] = deal (shuff); If you want to assign one of the values in shuff to each of the outputs then: Theme. Copy. shuff = num2cell (randperm (length (unshuff)));

  10. Organize Related Block Parameter Definitions in Structures

    Each structure is a single variable and each field of the structure stores a numeric parameter value. You can assign meaningful names to the structures, substructures, and fields to indicate the purpose of each value. ... For basic information about creating and manipulating MATLAB structures, see Structures.

  11. Converting Struct field to array

    Open in MATLAB Online. I have an n-value structure array with multiple fields, and want to extract a specific field (all values thereof are (1x4) single arrays) into a nx4 array. Theme. Copy. MyMatrix = [MyStruct (:).FieldC]; concatenates the values of the field, giving me a 1x (n*4) array. There's probably a way to reshape that, but I would ...

  12. Assign value to structure array field

    Create a field named title. S = setfield(S,{1}, 'title', 'y = sin(x)') S= 1×2 struct array with fields: x. y. title. The setfield function assigns a value to a field of an individual element, but the output argument is the entire structure array. Display the first element of S.

  13. getfield to get values from an entire struct array?

    I have an array of objects of type Peak. Given the name of a field (I will be using several in turn), I would like to get the value of that field for every object in the array, resulting in a vector of double. Is there any way to ask getfield to do that, without resorting to a for loop? Or is there some related function that would do the job ...

  14. assign cell arrays to struct

    Learn more about cell array, struct Dear all, is it somehow possible to assign cell arrays to the "field" and "value" variables within a struct, like test=struct(field,value) where "field" and "value" are cell arrays ?

  15. Structure array

    Between R2021b and R2023b, MATLAB ® displayed the entire contents of an array in a structure field if the contents fit in the display. If the array did not fit, MATLAB showed as much of the array as the display allowed. Starting in R2023b, if the entire contents of an array in a field do not fit in the display, MATLAB shows as much of the array as the display allows, as well as the size and ...

  16. How to assign a struct array to a field of another struct array?

    Copy. data % This is a struct array that doesnt have the field trait yet. trait % this is a struct array of the same length of data. assert (length (data) == length (trait)) data.t = trait % this fails with --> Scalar structure required for this assignment. [data.t] = [trait] % this fails with --> Insufficient number of outputs from right hand ...

  17. How to create a structure with fields from an array of strings in matlab?

    I have the following code, I'm trying to create an structure with field names from the cellarray of string. data1 has the following data, it's a 5x4: 1 5 298 53 2 9 284 35 3 ...

  18. Structure array

    A structure array is a data type that groups related data using data containers called fields.

  19. matlab

    I looked into it, but cell2struct seems to be designed to build struct arrays from cell arrays. What I want is a single struct with field names from the cell array. That said, ... MATLAB struct array field access. 2. Matlab convert struct to cell array of strings. 1. Using Cell Array Contents to Create Struct Entries. 2.