Clear Filters
Clear Filters

using structure as a value for containers.Map

12 views (last 30 days)
JA
JA on 26 Jul 2016
Answered: Guillaume on 27 Jul 2016
I am trying to add a structure as a value for container.Map, for example I'm trying to add segments strcuture to cont container.
Code:
Properties
segments = struct('signal', {}, 'time', {})
cont = containers.Map('KeyType','int32','ValueType','any');
end
fucntion:
if length(map) == 0
i = 2;
this.cont(1) = this.segments;
else
this.cont(i) = this.segments;
i = i+1;
end
Is this even possible, It adds fine, but when i try to retrieve the values.
cont.values(1)
I am getting:
Error using containers.Map/values
Parameter must be 'cell'.

Answers (1)

Guillaume
Guillaume on 27 Jul 2016
The valueSet returned by the values function is a cell array of whatever is stored in the map. Therefore,
cont.values{1}
would retrieve the 1st item stored in the map. If you're storing structures, then values{1} is a structure.
Important: using values is not the normal way to retrieve elements stored in the map. In particular, there is no guarantee that values{1} correspond to a key value of 1. To retrieve the value corresponding to a key value of 1:
valforkey1 = cont(1)
The two operations are vastly different. cont.values returns a cell array whose elements are in the same order as cont.keys, but there is no guarantee that cont.keys{1} has value 1.

Categories

Find more on Cell Arrays 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!