I have a cell array of strings of length 3
headers_ca =
{
[1,1] = time
[1,2] = x
[1,3] = y
}
I want to create a struct
that mimics a python dict, with the values in headers_ca
as keys (fieldnames in Octave) and an initializer value ival
for all entries.
It would be a struct
, since even dict
exists in octave, it has been deprecated.
I could do (brute force) s = struct("time", ival, "x", ival, "y", ival);
What is the most concise way to do this?
I know I can do a for loop. Can it be avoided?
I would be working with much longer cell arrays.
You can use struct or cell2struct to create the structure.
headers_ca = {'time','x','y'};
headers_ca(2, :) = {ival};
s = struct(headers_ca{:});
headers_ca = {'time','x','y'};
ivals = repmat({ival}, numel(headers_ca), 1);
s = cell2struct(ivals, headers_ca);