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 readfile
Home > sltoolbox > ExpDL > @dataset > readfile.m

readfile

PURPOSE ^

READFILE Reads the dataset from DSDML file

SYNOPSIS ^

function DS = readfile(DS, filename)

DESCRIPTION ^

READFILE Reads the dataset from DSDML file

 $ Syntax $
   - DS = readfile(DS, filename) 

 $ Arguments $
   - DS:           the dataset to be loaded from file
   - filename:     the DSDML file describing the dataset.

 $ Description $
   - DS = readfile(DS, filename) reads the dataset from the specified
     DSDML file and returns it.

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

CROSS-REFERENCE INFORMATION ^

This function calls:
  • xml_getattribs XML_GETATTRIBS Constructs an attribte struct from an XML element
This function is called by:
  • dataset DATASET Constructs a dataset object (conform to DSDML)

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function DS = readfile(DS, filename)
0002 %READFILE Reads the dataset from DSDML file
0003 %
0004 % $ Syntax $
0005 %   - DS = readfile(DS, filename)
0006 %
0007 % $ Arguments $
0008 %   - DS:           the dataset to be loaded from file
0009 %   - filename:     the DSDML file describing the dataset.
0010 %
0011 % $ Description $
0012 %   - DS = readfile(DS, filename) reads the dataset from the specified
0013 %     DSDML file and returns it.
0014 %
0015 % $ History $
0016 %   - Created by Dahua Lin on Jul 23rd, 2006
0017 %
0018 
0019 
0020 %% Read file
0021 
0022 xdoc = xmlread(filename);
0023 docelem = getDocumentElement(xdoc);
0024 
0025 %% Parse Header
0026 classname = class(DS);
0027 S = struct(DS);
0028 
0029 S.version = char(docelem.getAttribute('version'));
0030 S.name = char(docelem.getAttribute('name'));
0031 S.unittype = char(docelem.getAttribute('unit'));
0032 S.format = char(docelem.getAttribute('format'));
0033 if (docelem.hasAttribute('author'))
0034     S.author = char(docelem.getAttribute('author'));
0035 else
0036     S.author = [];
0037 end
0038 if (docelem.hasAttribute('description'))
0039     S.description = char(docelem.getAttribute('description'));
0040 else
0041     S.description = [];
0042 end
0043 S.attribs = xml_getattribs(docelem, 'exclude', {...
0044     'version', ...
0045     'name', ...
0046     'unit', ...
0047     'format', ...
0048     'author', ...
0049     'description' ...
0050     'attribs'});
0051 
0052 if ~ismember(S.unittype, {'Sample', 'SampleGroup'})
0053     error('dsdml:invalidutype', ...
0054         'Invalid unit type %s', S.unittype);
0055 end
0056 
0057 
0058 %% Get Units
0059 S.units = [];
0060 unitnodes = docelem.getElementsByTagName(S.unittype);
0061 numnodes = unitnodes.getLength;
0062 
0063 ucells = cell(numnodes, 1);
0064 
0065 switch(S.unittype)
0066     case 'Sample'
0067         for i = 1 : numnodes
0068             ucells{i} = CreateSample(unitnodes.item(i-1));
0069         end
0070         
0071     case 'SampleGroup'
0072         for i = 1 : numnodes
0073             ucells{i} = CreateSampleGroup(unitnodes.item(i-1));
0074         end
0075 end
0076 S.units = [ucells{:}];
0077 S.units = S.units(:);
0078 
0079 DS = class(S, classname);
0080 
0081 
0082 
0083 %%  Specific Functions
0084 
0085 function S = CreateSample(node)
0086 
0087 S.class_id = str2double(char(node.getAttribute('class_id')));
0088 S.filename = [];
0089 if node.hasAttribute('filename')
0090     S.filename = char(node.getAttribute('filename'));
0091 end
0092 S.attribs = xml_getattribs(node, 'exclude', {'class_id', 'filename', 'attribs'});
0093 
0094 
0095 function S = CreateSampleGroup(node)
0096 
0097 S.class_id = str2double(char(node.getAttribute('class_id')));
0098 S.attribs = xml_getattribs(node, 'exclude', {'class_id', 'attribs'});
0099 S.samples = [];
0100 
0101 samplenodes = node.getElementsByTagName('Sample');
0102 numsamples = samplenodes.getLength;
0103 scells = cell(numsamples, 1);
0104 for i = 1 : numsamples
0105     scells{i} = CreateSample(samplenodes.item(i-1));
0106 end
0107 
0108 S.samples = [scells{:}];
0109 S.samples = S.samples(:);
0110 
0111 
0112 
0113 
0114 
0115 
0116 
0117 
0118 
0119 
0120

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

Contact us at files@mathworks.com