# Is there any concept like dictionary or hash talbes in matlab like in Python?

1,749 views (last 30 days)
Aditya on 22 Jan 2014
Commented: Walter Roberson on 27 Jun 2020
help me with this.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

Amit on 22 Jan 2014
There is container.map http://www.mathworks.com/help/matlab/ref/containers.mapclass.html which is similar to dictionary in python.
##### 1 CommentShowHide None
Bill Tubbs on 27 Jun 2020

Sign in to comment.

### More Answers (3)

Lateef Adewale Kareem on 13 Feb 2017
Edited: Walter Roberson on 13 Feb 2017
Yes.
c = containers.Map
c('foo') = 1
c(' not a var name ') = 2
keys(c)
values(c)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

Vikram Raghu on 4 Apr 2019
struct
Works exactly the way I needed
##### 3 CommentsShowHide 2 older comments
Walter Roberson on 27 Jun 2020
Timing tests, see enclosed code.
The timings for the smaller structure, s2, are notably faster than for the bigger structure. This establishes that a perfect hash is not being used: a perfect hash would have constant access time regardless of the size of the structure.
The accesses for dynamic names.
Zeroth mean in output is measuring overhead of the algorithms: the structures are passed in and values are calculated, but without any reference to the structures.
First mean in output is measuring cost of numerous mostly-different dynamic field names. WIth the s2 (smaller structure) group being measurably faster compared to, we can see that dynamic field names cost more for larger structures.
Second, third, fourth means deal with the cost of accessing constant field names that are at various offsets into the structures. Fourth (furthest in the structure) is a little slower than third (40% into the structure) or second (beginning of the structure). The differences are not as high as one might expect, and on some of my tests, third (40% through) was faster than second (first), so there is some questions about statistical significance here. Fifth (all the way through) was always slower than first, though, in my tests.
Fifth is testing constant dynamic field name. With the larger structure (first group) it is consistently faster than random field names, but still much slower than constant field names. This tells us that the implementation is doing some kind of internal caching of structure name use.
The dynamic fieldname (test 1, 5) accesses are on the order of 10 times slower than the static fieldname accesses (test 2, 3, 4). This tells us that some kind of compilation is going on. At the same time, the fact that access to the last field (test 4) is slower than to the first field (test 2) shows that it is not compiled to constant offsets.
At the moment, I do not have a model implementation in mind.

Sign in to comment.

Bill Tubbs on 17 Apr 2020
Edited: Bill Tubbs on 17 Apr 2020
struct seems to produce a much nicer text output than container.Map:
% Example with struct
settings = struct();
settings.open_loop = false;
settings.adaptive = true;
settings.estimator = 'RLSFF';
if settings.open_loop
do something...
end
>> disp(settings)
open_loop: 0
adaptive: 1
estimator: 'RLSFF'
>> settings
settings =
struct with fields:
open_loop: 0
adaptive: 1
estimator: 'RLSFF'
% Example with Map
settings2 = containers.Map;
settings2('open_loop') = false;
settings2('adaptive') = true;
settings2('estimator') = 'RLSFF';
if settings2('open_loop')
do something...
end
>> settings2
settings2 =
Map with properties:
Count: 3
KeyType: char
ValueType: any
>> disp(settings)
open_loop: 0
adaptive: 1
estimator: 'RLSFF'
Although they look identical when returning at the command line.
But is there a literal representation for a struct? So you can define it in code more concisely, something like this:
settings = struct(
'open_loop': false,
'adaptive': true,
'estimator': 'RLSFF'
);
(The above is not valid of course). Or is there some other way of doing this in a readable convenient way?
##### 2 CommentsShowHide 1 older comment
Bill Tubbs on 20 Apr 2020
Your example doesn't work. Unfortunately, you need to add the line continuation syntax:
settings = struct(...
'open_loop', false, ...
'adaptive', true, ...
'estimator', 'RLSFF' ...
);

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!