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

来源:互联网 发布:数据分析 excel 编辑:程序博客网 时间:2024/05/15 14:59

参考: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  
0 0
原创粉丝点击