Code covered by the BSD License  

Highlights from
Statistical Learning Toolbox

from Statistical Learning Toolbox by Dahua Lin
Functions for statistical learning, pattern recognition and computer vision, covering many topics.

Description of writefile
Home > sltoolbox > ExpDL > @dataset > writefile.m

writefile

PURPOSE ^

WRITEFILE Writes a dataset to a DSDML file

SYNOPSIS ^

function writefile(DS, filename)

DESCRIPTION ^

WRITEFILE Writes a dataset to a DSDML file

 $ Syntax $
   - writefile(DS, filename)

 $ Arguments $
   - DS:           the dataset object
   - filename:     the name of the file to be written to

 $ Description $
   - writefile(DS, filename) writes a dataset object to the DSDML file
     with the filename specified.

 $ History $
   - Created by Dahua Lin on Jul 23rd, 2006

CROSS-REFERENCE INFORMATION ^

This function calls:
This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function writefile(DS, filename)
0002 %WRITEFILE Writes a dataset to a DSDML file
0003 %
0004 % $ Syntax $
0005 %   - writefile(DS, filename)
0006 %
0007 % $ Arguments $
0008 %   - DS:           the dataset object
0009 %   - filename:     the name of the file to be written to
0010 %
0011 % $ Description $
0012 %   - writefile(DS, filename) writes a dataset object to the DSDML file
0013 %     with the filename specified.
0014 %
0015 % $ History $
0016 %   - Created by Dahua Lin on Jul 23rd, 2006
0017 %
0018 
0019 import com.mathworks.xml.*;
0020 
0021 %% Initialize the document
0022 
0023 xdoc = XMLUtils.createDocument('DataSet');
0024 xdoc.setVersion('1.0');
0025 xdoc.setEncoding('UTF-8');
0026 
0027 S = struct(DS);
0028 
0029 
0030 %% Set headers
0031 
0032 docelem = xdoc.getDocumentElement;
0033 docelem.setAttribute('version', char(S.version));
0034 docelem.setAttribute('name', char(S.name));
0035 docelem.setAttribute('unit', char(S.unittype));
0036 docelem.setAttribute('format', char(S.format));
0037 if ~isempty(S.author)
0038     docelem.setAttribute('author', char(S.author));
0039 end
0040 if ~isempty(S.description)
0041     docelem.setAttribute('description', char(S.description));
0042 end
0043 if ~isempty(S.attribs)
0044     attrnames = fieldnames(S.attribs);
0045     nattrs = length(attrnames);
0046     for i = 1 : nattrs
0047         docelem.setAttribute(attrnames{i}, S.attribs.(attrnames{i}));
0048     end
0049 end
0050 
0051 
0052 %% Create unit nodes
0053 
0054 switch S.unittype
0055     case 'Sample'
0056         n = length(S.units);
0057         for i = 1 : n
0058             docelem.appendChild(CreateSampleNode(xdoc, S.units(i)));
0059         end
0060         
0061     case 'SampleGroup'
0062         n = length(S.units);
0063         for i = 1 : n
0064             docelem.appendChild(CreateSampleGroupNode(xdoc, S.units(i)));
0065         end
0066         
0067     otherwise
0068         error('dsdml:invalidutype', ...
0069             'Invalid unit type %s', DS.unittype);
0070 end
0071     
0072 
0073 %% Write document
0074 xmlwrite(filename, xdoc);
0075 
0076 
0077 
0078 %% Node Creating functions
0079 
0080 function sampleNode = CreateSampleNode(xdoc, sample)
0081 
0082 sampleNode = xdoc.createElement('Sample');
0083 sampleNode.setAttribute('class_id', int2str(sample.class_id));
0084 if ~isempty(sample.filename)
0085     sampleNode.setAttribute('filename', sample.filename);
0086 end
0087 if ~isempty(sample.attribs)
0088     attrnames = fieldnames(sample.attribs);
0089     nattrs = length(attrnames);
0090     for i = 1 : nattrs
0091         sampleNode.setAttribute(attrnames{i}, sample.attribs.(attrnames{i}));
0092     end
0093 end
0094 
0095 
0096 function groupNode = CreateSampleGroupNode(xdoc, grp)
0097 
0098 groupNode = xdoc.createElement('SampleGroup');
0099 groupNode.setAttribute('class_id', int2str(grp.class_id));
0100 if ~isempty(grp.attribs)
0101     attrnames = fieldnames(grp.attribs);
0102     nattrs = length(attrnames);
0103     for i = 1 : nattrs
0104         sampleNode.setAttribute(attrnames{i}, grp.attribs.(attrnames{i}));
0105     end
0106 end
0107 nsamples = length(grp.samples);
0108 for i = 1 : nsamples
0109     groupNode.appendChild(CreateSampleNode(xdoc, grp.samples(i)));
0110 end
0111 
0112 
0113 
0114 
0115 
0116

Generated on Wed 20-Sep-2006 12:43:11 by m2html © 2003

Contact us at files@mathworks.com