Initializing a MATLAB Map from an Array of Structures

My goal is to initialize a Map object with a double key and structure for the value.

I have a structural array similar to this:

s(1) = [1,2,3];
s(1) = 'str';
s(2) = [4,5,6];
s(2) = 'str2';
s(3) = [7,8,9];
s(3) = 'str3';

Then I want to create a map object, for example:

awesome = container.Map(1:3, s);

but I get the following error:

Error using containers.Map
Specified value type does not match the type expected for
this container.

But it works if I do:

awesome = container.Map(1:3, {s1, s2, s3});

Why is this happening and how can I make it work while preserving the structure as a value type?

0
source share
1 answer

Assuming your structure setup code should look something like this:

s(1).Numbers = [1,2,3];
s(1).String = 'str';
s(2).Numbers = [4,5,6];
s(2).String = 'str2';
s(3).Numbers = [7,8,9];
s(3).String = 'str3';

you can make a map from an array of structures using:

theMap = containers.Map( 1:3, arrayfun(@(x) ({x}), s))
0
source

Source: https://habr.com/ru/post/1624101/


All Articles