Help on creating structures??

1 次查看(过去 30 天)
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!

采纳的回答

Jacob Halbrooks
Jacob Halbrooks 2014-2-28
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by