Defining a Struct with Multiple Elements

In summary, the conversation discusses the creation of a structure with two elements, each with three fields: name, numKids, and kidsage. The first method of creating the structure in one line does not work as expected, leading to the suggestion of creating a cell array and converting it to a structure. The code for this is provided, along with a solution for adding a new element at a later time.
  • #1
Apteronotus
202
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
  • #2
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:
  • #3
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?
 
  • #4
Also, what if you wanted to add a new element at a later time. Say

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

1. What is a struct with multiple elements?

A struct with multiple elements is a data structure in programming that allows you to store and organize related data under a single variable. It is similar to a record or a class in other programming languages.

2. How do you define a struct with multiple elements?

To define a struct with multiple elements, you first need to create a custom data type using the struct keyword. Then, you can declare the different elements or fields within the struct, along with their data types and names. For example: struct Person {string name; int age;}

3. What is the purpose of a struct with multiple elements?

The purpose of a struct with multiple elements is to organize and group related data together for easier manipulation and access. It also allows for more efficient memory allocation and can make code more readable and maintainable.

4. Can a struct with multiple elements contain different data types?

Yes, a struct with multiple elements can contain different data types. This allows for more flexibility in storing different types of data under one variable. However, all elements within a single struct must be of a specific data type.

5. How do you access elements within a struct with multiple elements?

To access elements within a struct with multiple elements, you can use the dot (.) operator followed by the name of the element. For example, person.name would access the name element within the person struct. You can also use the arrow (->) operator when dealing with pointers to structs.

Similar threads

  • Programming and Computer Science
Replies
11
Views
2K
  • Programming and Computer Science
2
Replies
36
Views
3K
  • Set Theory, Logic, Probability, Statistics
Replies
16
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Special and General Relativity
Replies
29
Views
1K
  • Biology and Chemistry Homework Help
Replies
1
Views
619
  • Precalculus Mathematics Homework Help
Replies
11
Views
830
  • Linear and Abstract Algebra
Replies
5
Views
1K
  • Linear and Abstract Algebra
Replies
5
Views
1K
Replies
2
Views
289
Back
Top