Reading Data from container.Map Objects
Show older comments
I have created a container.Map object to store a structure as the value for a given key. The structure has two fields, the date which is a number and a remark for each date. While I could store this as a cell array (due to the different variable types), I like to use structures as it seems to help make the code more readable. I can populate the Map object with the data, unfortunately, when I try to list all of the remarks for a given key, it only returns one entry. What is the correct way to get a list of all the remarks for a given key?
I am running Matlab 8.1.0.604 (R2013a) with Java version 1.6.0_17-b04
Example code is as follows
% Create a new Map object
a = containers.Map;
% Create some silly data to store in it
times = datenum(2013, 1, 1, 0, 0, 0):1:datenum(2013, 1, 10, 0, 0, 0);
remarks = repmat({'Test String'}, 10, 1);
tmp = struct('Time', times, 'Remark', remarks);
% Insert the data into three different keys
a('One') = tmp;
a('Two') = tmp;
a('Three') = tmp;
% Now lets try to list all of the remarks associated with 'Two'
a('Two').Remark
I would like to be able to do something like:
{tmp.Remark}'
ans =
'Test String'
'Test String'
'Test String'
'Test String'
'Test String'
'Test String'
'Test String'
'Test String'
'Test String'
'Test String'
I would like to replace the tmp. in the last command to something like
{a('Two').Remark}'
When I do that I only get one line back. Thanks for any help on this.
Accepted Answer
More Answers (1)
per isakson
on 16 Jul 2013
Edited: per isakson
on 16 Jul 2013
This looks like a bug to me (R2012a). Either a('Two').Remark should return an error message or the list of ten strings (as you expect). But it should not return only the first string.
I fail to find anything in the documentation on
a('Two').Remark
It might not be supported to address a field of a structure, which is returned by a map-object, this way? Remains
z = a('Two');
z.Remark
5 Comments
Justace Clutter
on 17 Jul 2013
Sean de Wolski
on 17 Jul 2013
Edited: Sean de Wolski
on 17 Jul 2013
Also, it appears that structs and cells aren't supported for the valueType here:
This could be what is causing the problem, the struct is returned and containers.Map only looks at the first element. It is still strange that it works with Time, though the above could explain that.
Justace Clutter
on 17 Jul 2013
Edited: Justace Clutter
on 17 Jul 2013
Justace Clutter
on 17 Jul 2013
Sean de Wolski
on 17 Jul 2013
Edited: Sean de Wolski
on 17 Jul 2013
I think you're misinterpreting the doc, which I agree is kind of heavy:
vType
String that specifies the data type for the keys. Possible valuesare 'any', 'char', 'logical', 'double', 'single', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64',or 'uint64'.
Default: 'any' when you create an empty Map objector when you specify values of different sizes or types, otherwisedetermined by the data type of valueSet
The 'any', at least the way I'm understanding it (which might be incorrect!), is implying that you can combine any of the above classes, e.g. uint64 and double. It does not mean that any object or class can be stored.
And thanks for pointing out the typo, I'll report that to our doc team :)
Categories
Find more on Structures in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!