Assign value to structure array field (2024)

Assign value to structure array field

collapse all in page

Syntax

S = setfield(S,field,value)

S = setfield(S,field1,...,fieldN,value)

S = setfield(S,idx,field1,...,fieldN,value)

S = setfield(S,idx,field1,idx1,...,fieldN,idxN,value)

Description

example

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.

example

S = setfield(S,field1,...,fieldN,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.

example

S = setfield(S,idx,field1,...,fieldN,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.

example

S = setfield(S,idx,field1,idx1,...,fieldN,idxN,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.

Examples

collapse all

Assign Values to Fields

Open Live Script

Create a scalar structure.

S.x = linspace(0,2*pi);S.y = sin(S.x);S.title = ''
S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: ''

Assign a value to a field using the setfield function.

S = setfield(S,'title','y = sin(x)')
S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x)'

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

e = sqrt(abs(S.y));S = setfield(S,'sqrty',e)
S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x)' sqrty: [0 0.2518 0.3558 0.4350 0.5011 0.5586 0.6096 0.6556 0.6973 0.7353 0.7700 0.8017 0.8307 0.8571 0.8810 0.9025 0.9218 0.9389 0.9537 0.9665 0.9772 0.9858 0.9924 0.9969 0.9994 0.9999 0.9984 0.9949 0.9893 0.9818 0.9721 0.9604 ... ] (1x100 double)

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

S.title = 'y = sin(x), with error bar values'
S = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x), with error bar values' sqrty: [0 0.2518 0.3558 0.4350 0.5011 0.5586 0.6096 0.6556 0.6973 0.7353 0.7700 0.8017 0.8307 0.8571 0.8810 0.9025 0.9218 0.9389 0.9537 0.9665 0.9772 0.9858 0.9924 0.9969 0.9994 0.9999 0.9984 0.9949 0.9893 0.9818 0.9721 0.9604 ... ] (1x100 double)

Field of Nested Structure

Open Live Script

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.

S.a.b.c = 1;S.a.b.d = 2;S.a.b.e = struct('f',[3 4],'g',5);S.h = 50
S = struct with fields: a: [1x1 struct] h: 50

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

S.a
ans = struct with fields: b: [1x1 struct]
S.a.b
ans = struct with fields: c: 1 d: 2 e: [1x1 struct]
S.a.b.e
ans = struct with fields: f: [3 4] g: 5

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'.

S = setfield(S,'a','b','d',1024);S.a.b
ans = struct with fields: c: 1 d: 1024 e: [1x1 struct]

You also can use dot notation to assign a value.

S.a.b.d = 2048;S.a.b
ans = struct with fields: c: 1 d: 2048 e: [1x1 struct]

Fields of Elements of Structure Array

Open Live Script

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.

S.x = linspace(0,2*pi);S.y = sin(S.x);S(2).x = S.x;S(2).y = cos(S(2).x)
S=1×2 struct array with fields: x y

You also can assign values using setfield. If a field does not exist, setfield creates it. 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.

S(1)
ans = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [0 0.0634 0.1266 0.1893 0.2511 0.3120 0.3717 0.4298 0.4862 0.5406 0.5929 0.6428 0.6901 0.7346 0.7761 0.8146 0.8497 0.8815 0.9096 0.9341 0.9549 0.9718 0.9848 0.9938 0.9989 0.9999 0.9969 0.9898 0.9788 0.9638 0.9450 0.9224 ... ] (1x100 double) title: 'y = sin(x)'

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

S(2).title = 'y = cos(x)';S(2)
ans = struct with fields: x: [0 0.0635 0.1269 0.1904 0.2539 0.3173 0.3808 0.4443 0.5077 0.5712 0.6347 0.6981 0.7616 0.8251 0.8885 0.9520 1.0155 1.0789 1.1424 1.2059 1.2693 1.3328 1.3963 1.4597 1.5232 1.5867 1.6501 1.7136 1.7771 1.8405 1.9040 1.9675 ... ] (1x100 double) y: [1 0.9980 0.9920 0.9819 0.9679 0.9501 0.9284 0.9029 0.8738 0.8413 0.8053 0.7660 0.7237 0.6785 0.6306 0.5801 0.5272 0.4723 0.4154 0.3569 0.2969 0.2358 0.1736 0.1108 0.0476 -0.0159 -0.0792 -0.1423 -0.2048 -0.2665 -0.3271 ... ] (1x100 double) title: 'y = cos(x)'

Indices of Nested Structure Array

Open Live Script

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.

S.a = 1;S(2).a.b = struct('d',{5,10,20});S
S=1×2 struct array with fields: a
S(2).a.b
ans=1×3 struct array with fields: d

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

S(2).a.b(3)
ans = struct with fields: d: 20

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.

S = setfield(S,{2},'a','b',{3},'d',3.1416);S(2).a.b(3)
ans = struct with fields: d: 3.1416

Elements of Field

Open Live Script

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

S.a = [5 10 15 20 25]
S = struct with fields: a: [5 10 15 20 25]

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.

S = setfield(S,'a',{3:5},[0 -50 -100])
S = struct with fields: a: [5 10 0 -50 -100]

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

S.a(3:5) = [20 40 80]
S = struct with fields: a: [5 10 20 40 80]

Input Arguments

collapse all

SStructure 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.

fieldField name
character vector | string scalar

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

idxIndices
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].

valueValues
array

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

Extended Capabilities

Version History

Introduced before R2006a

See Also

getfield | fieldnames | isfield | orderfields | rmfield | struct

Topics

  • 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.

Assign value to structure array field (1)

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:

Americas

  • América Latina (Español)
  • Canada (English)
  • United States (English)

Europe

  • 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)
  • Switzerland
    • Deutsch
    • English
    • Français
  • United Kingdom (English)

Asia Pacific

  • Australia (English)
  • India (English)
  • New Zealand (English)
  • 中国
  • 日本 (日本語)
  • 한국 (한국어)

Contact your local office

Assign value to structure array field (2024)
Top Articles
How To Get Free Coins in the Line Chat App
Chat Zone Review & Users Opinion 2021
Frederick County Craigslist
Maria Dolores Franziska Kolowrat Krakowská
Online Reading Resources for Students & Teachers | Raz-Kids
T Mobile Rival Crossword Clue
Chelsea player who left on a free is now worth more than Palmer & Caicedo
Craigslist Mexico Cancun
Student Rating Of Teaching Umn
Brutál jó vegán torta! – Kókusz-málna-csoki trió
Costco Gas Foster City
Craigslist Panama City Fl
Golden Abyss - Chapter 5 - Lunar_Angel
Mikayla Campinos Laek: The Rising Star Of Social Media
Wsop Hunters Club
Craigslist Lakeville Ma
Mc Donald's Bruck - Fast-Food-Restaurant
Shreveport City Warrants Lookup
Anotherdeadfairy
48 Oz Equals How Many Quarts
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Dashboard Unt
Rainfall Map Oklahoma
Korg Forums :: View topic
N.J. Hogenkamp Sons Funeral Home | Saint Henry, Ohio
Dailymotion
Ilabs Ucsf
Learn4Good Job Posting
Was heißt AMK? » Bedeutung und Herkunft des Ausdrucks
Lehpiht Shop
Here’s how you can get a foot detox at home!
Old Peterbilt For Sale Craigslist
Vip Lounge Odu
Family Fare Ad Allendale Mi
Latest Nigerian Music (Next 2020)
Oxford House Peoria Il
Indiana Jones 5 Showtimes Near Cinemark Stroud Mall And Xd
Carroll White Remc Outage Map
Janaki Kalaganaledu Serial Today Episode Written Update
Three V Plymouth
Weather In Allentown-Bethlehem-Easton Metropolitan Area 10 Days
Payrollservers.us Webclock
Powerboat P1 Unveils 2024 P1 Offshore And Class 1 Race Calendar
Best Conjuration Spell In Skyrim
Shannon Sharpe Pointing Gif
Ewwwww Gif
The top 10 takeaways from the Harris-Trump presidential debate
Concentrix + Webhelp devient Concentrix
Diamond Desires Nyc
View From My Seat Madison Square Garden
King Fields Mortuary
Laurel Hubbard’s Olympic dream dies under the world’s gaze
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5821

Rating: 4.8 / 5 (58 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.