Defining a Struct with Multiple Elements

  • Context: MATLAB 
  • Thread starter Thread starter Apteronotus
  • Start date Start date
  • Tags Tags
    Elements Multiple
Click For Summary

Discussion Overview

The discussion revolves around defining a structure in MATLAB with multiple elements, specifically focusing on how to create and manipulate structures one field at a time. Participants explore different methods for achieving this, including the use of cell arrays and the conversion to structures.

Discussion Character

  • Technical explanation
  • Exploratory
  • Homework-related

Main Points Raised

  • One participant presents an initial attempt to define a structure with multiple elements in a single line and then seeks to do so one field at a time.
  • Another participant suggests that the functionality implied by the documentation may not exist in version R2009a, proposing the use of a cell array followed by conversion to a structure as an alternative approach.
  • A participant expresses gratitude for the suggested code that successfully produces the desired results and inquires about the failure of their original code.
  • There is a question raised about how to add a new element to the structure after its initial creation.
  • A later reply provides a method for adding a new element to the structure using a specific syntax.
  • Another participant acknowledges the workaround provided and shares their past difficulties with similar issues.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the effectiveness of the original code, as one participant successfully implements an alternative method while another questions the functionality of their approach. The discussion includes multiple perspectives on how to define and manipulate structures.

Contextual Notes

Limitations include potential misunderstandings of the documentation, version-specific functionality, and the implications of using cell arrays versus direct structure definitions.

Who May Find This Useful

Readers interested in MATLAB programming, particularly those working with data structures and seeking solutions for defining and manipulating structures effectively.

Apteronotus
Messages
201
Reaction score
0
Hi

The following structure has 2 elements.

people = struct(...
'name',{'bob', 'john'},...
'numKids',{0, 2}, ...
'kidsage',{[],[12,9]});

each element people(1), people(2) has three frields (name, numKids, kidsage)

Instead of declaring the structure in one line as above, I would like to define it one field at a time.
The closest working example I've been able to come up with is

people = struct

people.('name') = {'bob, 'john'}
people.('numkids') = {0, 2}
people.('kidsage') = {[], [12, 9]}

But defined in this way, the people structure only has one element.
ie.
people(1) =

name: {'bob' 'john'}
numkids: {[0] [2]}
kidsage: {[] [12, 9]}

Can anyone help?
 
Physics news on Phys.org
Although the documentation might lead one to believe this functionality exists, (from the text under the heading "Specifying Cell Versus Noncell Values"), it does not seem to exist, at least with version R2009a.

Instead, one may create a cell array and then convert it to a structure.

Code:
people_cell_array = cell(2, 3);

people_cell_array(:, 1) = {'bob' ; 'john'};
people_cell_array(:, 2) = {0 ; 2};
people_cell_array(:, 3) = {[] ; [12, 9]};

people = cell2struct(people_cell_array, {'name', 'num_kids', 'kids_age'}, 2);
 
Last edited:
MisterX
Thank you. Your code definitely produces the results that I was looking for.
Do you have any idea why my original code didnt work?
 
Also, what if you wanted to add a new element at a later time. Say

name: 'Mike'
num_kids: 1
kids_age: 3
 
Code:
people(3) = struct('name', 'Mike', 'num_kids', 1, 'kids_age', 3);
 
Fantastic Mr X, I've had issues with this in the past, nice workaround.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
5K
Replies
1
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 10 ·
Replies
10
Views
4K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 26 ·
Replies
26
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 75 ·
3
Replies
75
Views
7K
  • · Replies 8 ·
Replies
8
Views
2K