Converting Matlab to Octave is there a containers.Map equivalent?

后端 未结 1 1172
别跟我提以往
别跟我提以往 2021-01-11 14:50

I am trying to convert some matlab code from the Maia package into something that will work with Octave. I am currently getting stuck because one of the files has several ca

相关标签:
1条回答
  • 2021-01-11 15:17

    There is no exact equivalent of containers.Map in Octave that I know of...

    One option is to use the java package to create java.util.Hashtable. Using this example:

    pkg load java
    d = javaObject("java.util.Hashtable");
    d.put('a',1)
    d.put('b',2)
    d.put('c',3)
    d.get('b')
    

    If you are willing to do a bit of rewriting, you can use the builtin struct as a rudimentary hash table with strings (valid variable names) as keys, and pretty much anything stored in values.

    For example, given the following:

    keys = {'Mon','Tue','Wed'}
    values = {10, 20, 30}
    

    you could replace this:

    map = containers.Map(keys,values);
    map('Mon')
    

    by:

    s = struct();
    for i=1:numel(keys)
        s.(keys{i}) = values{i};
    end
    s.('Mon')
    

    You might need to use genvarname to produce valid keys, or maybe a proper hashing function that produces valid key strings.

    Also look into struct-related functions: getfield, setfield, isfield, fieldnames, rmfield, etc..

    0 讨论(0)
提交回复
热议问题