Help on creating structures??

1 view (last 30 days)
Mary
Mary on 28 Feb 2014
Answered: Jacob Halbrooks on 28 Feb 2014
The only way I know how to create a structure where all of my sections are ordered is like so:
struct1(1).name='Krista';
struct1(2).name='Kranthi';
struct1(3).name='Kevin';
struct1(4).name='Kalil';
struct1(5).name='Kristen';
struct1(1).date=[10 5 1993];
struct1(2).date=[6 16 1990];
struct1(3).date=[4 20 1991];
struct1(4).date=[7 10 1819];
struct1(5).date=[2 31 1992];
Is there are more concise way to write the code for this and still get the same result?? Help appreciated :) thank you!

Accepted Answer

Jacob Halbrooks
Jacob Halbrooks on 28 Feb 2014
You could enter your data into a two-column cell array and then use CELL2STRUCT:
data = {'Krista', [10 5 1993]; ...
'Kranthi', [6 16 1990]; ...
'Kevin', [4 20 1991]; ...
'Kalil', [7 10 1819]; ...
'Kristen', [2 31 1992]};
s = cell2struct(data, {'name', 'date'},2)

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!