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!

1

2 Answers 2

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
>> 
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! It can also be inherited if one needs more methods to add. Very nice!
+1 -- I've been using Matlab for 12 yrs and didn't know about that one.
@Jason S: Don't feel bad; it's new as of R2008b, and not well advertised in the doco or otherwise. I just stumbled across it last year.
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.

5 Comments

@Jason, Could you show an example how you would make a simple hash-array in MATLAB? I have no experience with Java
@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.
@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.
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, It takes a bit more for me :) You can have a look at stackoverflow.com/questions/3591942/hash-tables-in-matlab There are links to MATLAB Central with ready examples.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.