Reading Data from container.Map Objects
4 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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.
0 Commenti
Risposta accettata
Sean de Wolski
il 17 Lug 2013
I think I got it while writing the comment above.
v = a('Two')
Returns a 10x1 struct. If you look at the output from
v.Time
you'll see ans 10 times, each time with all 10 values.
If you look at:
v.Remark
You'll see test string printed for ans 10x. What is happening is containers.Map's subsref is only extracting the last value since it is not supported for cells or structs. This is just like it would appear at the command line:
y = v.Remark
z = v.Time
Either way, I think this is a fair enhancement request. Why do you want to nest structures inside of a containers.Map object?
3 Commenti
Sean de Wolski
il 17 Lug 2013
The OOP approach is that you suggest above is what I would recommend.
Alternatively, if containers.Map is not sealed, you could inherit from it and then overload subsref to support structs and cells.
Più risposte (1)
per isakson
il 16 Lug 2013
Modificato: per isakson
il 16 Lug 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 Commenti
Sean de Wolski
il 17 Lug 2013
Modificato: Sean de Wolski
il 17 Lug 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 :)
Vedere anche
Categorie
Scopri di più su Dictionaries in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!