參考:http://blog.csdn.net/wzy1990/article/details/8508662
% name是輸入的文件名,data是matlab中的矩陣
function createxml(name,datatest)
xdoc=com.mathworks.xml.XMLUtils.createDocument('opencv_storage');xroot=xdoc.getDocumentElement;
%
[m,n]=size(datatest);
type=xdoc.createElement(name);
xroot.appendChild(type);
type.setAttribute('type_id','opencv-matrix')
%
rows=xdoc.createElement('rows');
rows.appendChild(xdoc.createTextNode(sprintf('%d',m)));
type.appendChild(rows);
cols=xdoc.createElement('cols');
cols.appendChild(xdoc.createTextNode(sprintf('%d',n)));
type.appendChild(cols);
dt=xdoc.createElement('dt');
dt.appendChild(xdoc.createTextNode(sprintf('%s','f')));
type.appendChild(dt);
//寫入數據
data=xdoc.createElement('data');
data.appendChild(xdoc.createTextNode(sprintf('%10.8f ',datatest)));
type.appendChild(data);
//或者一下注釋的這段
% for i=1:m% for j=1:n
% data.appendChild(xdoc.createTextNode(sprintf('%10.8f ',datatest(i,j))));
% end
% end
% type.appendChild(data);
xmlwrite(str,xdoc);
end