MATLAB: How to initialise a struct array with pairs

arraydatafieldinitializationstruct

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?

Best Answer

One possible way:
pairs = {'TJ', 'Tom Jones';
'JS', 'John Smith'};
data = struct('shortname', pairs(:, 1), 'longname', pairs(:, 2))
Alternatively:
pairs = {'TJ', 'Tom Jones';
'JS', 'John Smith'};
data = cell2struct(pairs, {'shortname', 'longname'}, 2);
In any case, start with a cell array and convert to structure.