matlab下生成的矩陣存入xml文件中

參考: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);


str=strcat(name,'.xml');  
xmlwrite(str,xdoc);  
end  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章