您需要確保從MATLAB傳遞到Java的數據可以正確轉換。查看MATLAB的External Interfaces document獲取哪些類型轉換爲哪些類型的轉換矩陣。
MATLAB將大多數數據視爲傳遞值(除了具有句柄語義的類),並且似乎沒有辦法在Java接口中包裝結構。但是您可以使用另一個HashMap來表現結構,並將MATLAB結構轉換爲HashMaps(對於多層結構,函數句柄,以及其他與MATLAB/Java數據轉換過程無關的其他野獸) 。
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
一個可能的使用情況:
>> M = java.util.HashMap;
>> M.put(1,'a');
>> M.put(2,33);
>> s = struct('a',37,'b',4,'c','bingo')
s =
a: 37
b: 4
c: 'bingo'
>> M.put(3,struct2hashmap(s));
>> M
M =
{3.0={a=37.0, c=bingo, b=4.0}, 1.0=a, 2.0=33.0}
>>
(一個練習讀者:更改爲遞歸結構構件,其本身是結構工作)
但據我所知,它們不適合作爲Java Map <>參數的參數? – glglgl 2015-02-20 13:21:55