how to initialise a struct array with pairs?
Show older comments
I want to keep pairs next to each other during initialisation.
The result what I want is something like this:
data(1).shortname = 'TJ';
data(1).longname = 'Tom Jones';
data(2).shortname = 'JS';
data(2).longname = 'John Smith';
...
But I want to initialise this struct array similar to the following method somehow:
data = ... 'TJ', 'Tom Jones', 'JS', 'John Smith', ...
or
data = ... {'TJ', 'Tom Jones'}, {'JS', 'John Smith'}, ...
Is it possible?
Accepted Answer
More Answers (2)
Thorsten
on 6 Oct 2015
Initialise
data(1).name = {'TJ', 'Tom Jones'};
data(2).name = {'JS', 'John Smith'};
Get entries
data(1).name{1}
ans =
TJ
>> data(1).name{2}
ans =
Tom Jones
Andrei Bobrov
on 6 Oct 2015
Edited: Andrei Bobrov
on 6 Oct 2015
out = num2cell(reshape(struct2cell(data),2,[])',2)
Categories
Find more on Data Type Conversion 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!