Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to ensure that the data passed from MATLAB to Java can be properly converted. See MATLAB's <a href="http://www.mathworks.com/help/pdf_doc/matlab/apiext.pdf" rel="nofollow noreferrer">External Interfaces document</a> for the conversion matrix of which types get converted to which other types.</p> <p>MATLAB treats most data as pass-by-value (with the exception of classes with handle semantics), and there doesn't appear to be a way to wrap a structure in a Java interface. But you could use another HashMap to act like a structure, and convert MATLAB structures to HashMaps (with an obvious warning for multiple-level structures, function handles, + other beasts that don't play well with the MATLAB/Java data conversion process).</p> <pre><code>function hmap = struct2hashmap(S) if ((~isstruct(S)) || (numel(S) ~= 1)) error('struct2hashmap:invalid','%s',... 'struct2hashmap only accepts single structures'); end hmap = java.util.HashMap; for fn = fieldnames(S)' % fn iterates through the field names of S % fn is a 1x1 cell array fn = fn{1}; hmap.put(fn,getfield(S,fn)); end </code></pre> <p>a possible use case:</p> <pre><code>&gt;&gt; M = java.util.HashMap; &gt;&gt; M.put(1,'a'); &gt;&gt; M.put(2,33); &gt;&gt; s = struct('a',37,'b',4,'c','bingo') s = a: 37 b: 4 c: 'bingo' &gt;&gt; M.put(3,struct2hashmap(s)); &gt;&gt; M M = {3.0={a=37.0, c=bingo, b=4.0}, 1.0=a, 2.0=33.0} &gt;&gt; </code></pre> <p>(an exercise for the reader: change this to work recursively for structure members which themselves are structures)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload