1

Is it possible to overload subsref and subsasgn to allow non-integer types for index value?

h = Hash; #% a custom hash class to manage my data
h(100) = 'data'; #% integer is fine, if index > 0

h{'string'} #% but this fails
??? Cell contents reference from a
non-cell array object.

Can one hack it somehow?


Exact solution:

There are several annoyances in containers.Map, which can be solved by making a custom class which inherits it:

classdef Hash < containers.Map
  # fun
end

In such class one can implement various types of keys (not just one!!) and convenience methods for the user operations. Also it is possible to redefine subsref and subsasgn to work with curly braces and multiple indices. Nice!

Community
  • 1
  • 1
Andrei Fokau
  • 225
  • 1
  • 4
  • 11
  • 2
    There is a built in solution too: 'Containers.Map'.Have a look at http://stackoverflow.com/questions/3591942/hash-tables-in-matlab – zellus Dec 13 '10 at 16:14

2 Answers2

6

No need to hack. Use a struct or a containers.Map. They are native Matlab data structures for associative arrays. A struct is indexed by strings (with some restrictions). A containers.Map can be indexed by string, non-integer numerics, or other data types. See "help struct" and "help containers.Map". The Map uses parentheses for indexing, so its syntax looks like an array indexed by other means.

>> m = containers.Map(.1, 'myvalue');
>> m(.75) = 'anothervalue';
>> x = m(.1)
x =
myvalue
>> 
Jason S
  • 184,598
  • 164
  • 608
  • 970
Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
0

Why not just use a java.util.HashMap? Matlab works fine with Java. (Although I guess that only works with data that can be marshalled into Java, so although matrices and cell arrays of matrices are OK, structs are out)

>> x = java.util.HashMap;
>> x.put(3, [1 2 3]);
>> x.put('Rosebud',[4 5 6; 7 8 9]);
>> x.put([2 4 6 8],'Michaelangelo'); 
>> x.get(3)

ans =

     1
     2
     3

>> x.get('Rosebud')

ans =

     4     5     6
     7     8     9

>> x.get([2 4 6 8])

ans =

     []

Aha: watch out for that last bit -- the equality semantics in Java for numbers and strings are straightforward, but for arrays, things get tricky, and using Java in MATLAB is a little like handling lab samples in a glove box.

If you can deal with the limitations of java.util.HashMap (key equality semantics, type limitations to those that can be marshalled between Java and MATLAB), use it -- otherwise you probably would have to write your own.

Jason S
  • 184,598
  • 164
  • 608
  • 970
  • @Jason, Could you show an example how you would make a simple hash-array in MATLAB? I have no experience with Java – Andrei Fokau Dec 13 '10 at 15:40
  • @Jason, Thanks, there are probably many advantages in the Java class. However, I wanted to avoid method calls (get/set) and to use just brackets. – Andrei Fokau Dec 13 '10 at 15:46
  • @Jason, It takes a couple of minutes to make a Hash class with .get and .set methods which would work better than `java.util.HashMap`. – Andrei Fokau Dec 13 '10 at 15:50
  • cool, if you don't mind, could you post? I haven't got used to the new MATLAB class syntax, + it would be a good example. – Jason S Dec 13 '10 at 15:51
  • @Jason, It takes a bit more for me :) You can have a look at http://stackoverflow.com/questions/3591942/hash-tables-in-matlab There are links to MATLAB Central with ready examples. – Andrei Fokau Dec 13 '10 at 16:35