From: <cde...@us...> - 2012-09-05 10:21:17
|
Revision: 10964 http://octave.svn.sourceforge.net/octave/?rev=10964&view=rev Author: cdemills Date: 2012-09-05 10:21:06 +0000 (Wed, 05 Sep 2012) Log Message: ----------- Header values now stored in a 'header' field, plus various cleanup Modified Paths: -------------- trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m 2012-09-05 08:41:17 UTC (rev 10963) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/dataframe.m 2012-09-05 10:21:06 UTC (rev 10964) @@ -1,4 +1,4 @@ -function df = dataframe(x = [], varargin) +function df = dataframe(x, varargin) %# -*- texinfo -*- %# @deftypefn {Function File} @var{df} = dataframe(@var{x = []}, ...) @@ -61,14 +61,10 @@ %# if (0 == nargin) - disp ('FIXME -- should create a dataframe from the whole workspace') - df = dataframe ([]); - return -endif - -if (isempty (x) && 1 == nargin) - %# default constructor: initialise the fields in the right order - df._cnt = [0 0]; + %# default constructor: create a scalar struct and initialise the + %# fields in the right order + df = struct ('_cnt', [0 0]); + %# do not call "struct" with the two next args: it would create an array df._name = {cell(0, 1), cell(1, 0)}; %# rows - cols df._over = cell (1, 2); df._ridx = []; @@ -76,24 +72,45 @@ df._rep = cell (0, 0); %# a repetition index df._type = cell (0, 0); %# the type of each column df._src = cell (0, 0); + df._header = cell (0, 0); df._cmt = cell (0, 0); %# to put comments df = class (df, 'dataframe'); return endif +if (isnull (x) && 1 == nargin) + disp ('FIXME -- should create a dataframe from the whole workspace') + df = dataframe (); %# just call the default constructor + return +endif + if (isa (x, 'dataframe')) + %# Try to append whatever data or metadata given through varargin + %# into this dataframe df = x; -elseif (isa (x, 'struct')) - df = class (x, 'dataframe'); return else - df = dataframe ([]); %# get the right fields + df = dataframe (); %# get the right fields + if (isa (x, 'struct')) + %# only accept a struct if it has the same fieldnames as a dataframe + dummy = fieldnames (x); + indi = fieldnames (df); + if (size (dummy, 1) ~= size (indi, 1)) + error ('First argument of dataframe is a struct with the wrong number of fields'); + endif + if (~all (cellfun (@strcmp, dummy, indi))) + error ('First argument of dataframe is a struct with wrong field names'); + endif + %# easy way to transform a struct into a dataframe object + df = class (x, 'dataframe'); + if (1 == nargin) return; endif + endif endif %# default values seeked = []; trigger = []; unquot = true; sep = "\t,"; cmt_lines = []; locales = "C"; datefmt = ''; -if (length (varargin) > 0) +if (length (varargin) > 0) %# extract known arguments indi = 1; %# loop over possible arguments while (indi <= size (varargin, 2)) @@ -149,7 +166,11 @@ case 'datefmt' datefmt = varargin{indi + 1}; varargin(indi:indi+1) = []; - otherwise %# FIXME: just skip it for now + case '--' + %# stop processing args -- take the rest as filenames + varargin(indi) = []; + break; + otherwise %# FIXME: just skip it for now disp (sprintf ("Ignoring unkown argument %s", varargin{indi})); indi = indi + 1; endswitch @@ -181,16 +202,15 @@ indi = indi + 1; if (~isa (x, 'dataframe')) if (isa (x, 'char') && size (x, 1) < 2) - %# read the data frame from a file + dummy = tilde_expand (x); try - dummy = tilde_expand (x); + %# read the data frame from a file x = load (dummy); df._src{end+1, 1} = dummy; catch %# try our own method UTF8_BOM = char ([0xEF 0xBB 0xBF]); unwind_protect - dummy = tilde_expand (x); fid = fopen (dummy); if (fid ~= -1) df._src{end+1, 1} = dummy; @@ -244,9 +264,14 @@ continue; endif dummy = content{indl}; - if (strcmp (dummy{1}, seeked)) + if (~isempty (regexp (dummy{1}, seeked, 'match'))) break; - endif + endif + if (isempty (df._header)) + df._header = dummy; + else + df._header(end+1, 1:length (dummy)) = dummy; + endif indl = indl + 1; endwhile elseif (~isempty (trigger)) @@ -256,7 +281,12 @@ if (all (cellfun ('size', dummy, 2) == 0)) continue; endif - if (size (dummy, 2) >= 1 && ... + if (isempty (df._header)) + df._header = dummy; + else + df._header(end+1, 1:length (dummy)) = dummy; + endif + if (size (dummy, 2) >= 1 && ... ~isempty (regexp (dummy{1}, trigger, 'match'))) break; endif @@ -430,7 +460,11 @@ %# use direct assignement if (ndims (x) > 2), idx.subs{3} = 1:size (x, 3); endif %# df = subsasgn(df, idx, x); <= call directly lower level - df = df_matassign (df, idx, indj, length (indj), x); + try + df = df_matassign (df, idx, indj, length (indj), x); + catch + disp ('line 443 '); keyboard + end_try_catch if (~isempty (cmt_lines)) df._cmt = vertcat (df._cmt, cellstr (cmt_lines)); cmt_lines = []; Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m 2012-09-05 08:41:17 UTC (rev 10963) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/subsasgn.m 2012-09-05 10:21:06 UTC (rev 10964) @@ -117,6 +117,14 @@ endif return + case "header" + if (length (S) > 1) + df._header = feval (@subsasgn, df._header, S(2:end), RHS); + else + df._header = RHS; + endif + return + case "comment" if (length (S) > 1) df._cmt = feval (@subsasgn, df._cmt, S(2:end), RHS); Modified: trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m =================================================================== --- trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m 2012-09-05 08:41:17 UTC (rev 10963) +++ trunk/octave-forge/extra/dataframe/inst/@dataframe/subsref.m 2012-09-05 10:21:06 UTC (rev 10964) @@ -124,6 +124,9 @@ case "source" S(1).subs = "_src"; further_deref = true; + case "header" + S(1).subs = "_header"; + further_deref = true; case "comment" S(1).subs = "_cmt"; further_deref = true; @@ -431,6 +434,7 @@ endif %# to be verified : keyboard resu._src = df._src; + resu._header = df._header; resu._cmt = df._cmt; resu = df_thirddim (resu); if (length (S) > 1) %# perform further access, if required This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |