天天看點

使用matlab将mat矩陣存儲為xml檔案

參考連結如下:

原文

改後

function createxml(name1,mat1) 
% name是輸入的檔案名,datatest是matlab中的矩陣(一般都是float格式存儲的)
% name2,datatest2
xdoc=com.mathworks.xml.XMLUtils.createDocument('opencv_storage');
xroot=xdoc.getDocumentElement;

%========定義矩陣,寫入xml
[m1,n1]=size(mat1);

type=xdoc.createElement(name1);
xroot.appendChild(type);
type.setAttribute('type_id','opencv-matrix')

rows=xdoc.createElement('rows');
rows.appendChild(xdoc.createTextNode(sprintf('%d',m1)));
type.appendChild(rows);

cols=xdoc.createElement('cols');
cols.appendChild(xdoc.createTextNode(sprintf('%d',n1)));
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('%f ',mat1)));
type.appendChild(data);
%=======結束寫入

str=strcat(name1,'.xml');
xmlwrite(str,xdoc);
end

           

繼續閱讀