Home > utility > tableRead.m

tableRead

PURPOSE ^

tableRead: Read a table with tab-separated fields

SYNOPSIS ^

function table=tableRead(fileName, format, fieldNames, delimiter)

DESCRIPTION ^

 tableRead: Read a table with tab-separated fields
    Usage: table=tableRead(fileName, format, fieldNames, delimiter)
        fileName: table file name
        format: 1 (see table01.txt), 2 (see table02.txt), or 3 (see table03.txt)
        fieldNames: names of fields (for type-1 format only)

    For example:
        table=tableRead('table01.txt', 1, {'id', 'word', 'count'})

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SUBFUNCTIONS ^

SOURCE CODE ^

0001 function table=tableRead(fileName, format, fieldNames, delimiter)
0002 % tableRead: Read a table with tab-separated fields
0003 %    Usage: table=tableRead(fileName, format, fieldNames, delimiter)
0004 %        fileName: table file name
0005 %        format: 1 (see table01.txt), 2 (see table02.txt), or 3 (see table03.txt)
0006 %        fieldNames: names of fields (for type-1 format only)
0007 %
0008 %    For example:
0009 %        table=tableRead('table01.txt', 1, {'id', 'word', 'count'})
0010 
0011 %    Roger Jang, 20051111, 20071009
0012 
0013 if nargin<1, selfdemo; return; end
0014 if nargin<4, delimiter=9; end        % \t as the default delimiter
0015 
0016 splitMode=1;
0017 switch format
0018     case 1
0019         contents=textread(fileName, '%s', 'delimiter', '\n', 'whitespace', '');
0020         table=[];
0021         for i=1:length(contents)
0022             line=contents{i};
0023         %    fprintf('%d/%d: line=%s\n', i, length(contents), line);
0024             items=split(line, delimiter, splitMode);        % tab separated
0025         %    keyboard
0026             itemNum=min(length(items), length(fieldNames));        % Assign parsed items
0027             for j=1:itemNum
0028                 table=setfield(table, {i}, fieldNames{j}, items{j}); 
0029             end
0030         end
0031     case 2
0032         contents=textread(fileName, '%s', 'delimiter', '\n', 'whitespace', '');
0033         fieldNames=split(contents{1}, delimiter, splitMode);
0034         table=[];
0035         for i=2:length(contents)
0036             line=contents{i};
0037             items=split(line, delimiter, splitMode);        % tab separated
0038             itemNum=min(length(items), length(fieldNames));        % Assign parsed items
0039             for j=1:itemNum
0040                 table=setfield(table, {i-1}, fieldNames{j}, items{j}); 
0041             end
0042         end
0043     case 3
0044         contents=textread(fileName, '%s', 'delimiter', '\n', 'whitespace', '');
0045         table=[];
0046         for i=1:length(contents)
0047             line=contents{i};
0048             items=split(line, delimiter, splitMode);        % '\t' as the delimiter
0049             for j=1:length(items),
0050                 temp=split(items{j}, '=');
0051                 field=temp{1};
0052                 if length(temp)==2
0053                     value=temp{2};
0054                 else
0055                     value='';
0056                 end
0057                 table=setfield(table, {i}, field, {1:length(value)}, value);
0058             end
0059         end
0060     otherwise
0061         disp('Unknown format!');
0062 end
0063 
0064 % ====== self demo
0065 function selfdemo
0066 table01 = tableRead('table01.txt', 1, {'id', 'word', 'count'});
0067 dispStructInHtml(table01);
0068 fprintf('Hit any key to view table02...'); pause; fprintf('\n');
0069 table02 = tableRead('table02.txt', 2);
0070 dispStructInHtml(table02);
0071 fprintf('Hit any key to view table03...'); pause; fprintf('\n');
0072 table03 = tableRead('table03.txt', 3);
0073 dispStructInHtml(table03);

Generated on Tue 01-Jun-2010 09:50:34 by m2html © 2003