using structure as a value for containers.Map

9 次查看(过去 30 天)
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'.

回答(1 个)

Guillaume
Guillaume 2016-7-27
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.

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by