|
From: Mark P. E. <ma...@sr...> - 2004-05-07 19:20:09
|
I had a need to import into octave text files with with headers. I modified
dlmread.m so that it could skip over lines of text before reading the data.
The addition should be compatible with Matlab even though it doesn't
implement all the functionality. I don't have Matlab so I couldn't check for
sure. I also fixed a bug in dlmread that made it so some deliminators like
";" wouldn't work. I have attached a patch. Feel free to modify it as
needed, I am not very experienced at programing m-files.
-Mark Esplin
--- /usr/local/share/octave/2.1.57/site/m/octave-forge/io/dlmread.m
2004-05-07 15:00:21.000000000 -
0400
+++ dlmread.m 2004-05-07 14:59:39.000000000 -0400
@@ -14,9 +14,10 @@
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
-## x = dlmread (filename, sep)
+## x = dlmread (filename, sep, row)
## Read the matrix x from a file, with columns separated by the
## character sep (default is ","). NaN values are written as nan.
+## The number of rows to skip before reading data is row (default is 0).
##
## WARNING: for compatibility, must treat empty fields as zero, but doesn't.
@@ -24,12 +25,13 @@
## 2001-02-16
## * first revision
-function x = dlmread (filename, sep)
+function x = dlmread (filename, sep, row)
- if (nargin < 1 || nargin > 2)
- usage ("x = dlmread (filename, sep)");
+ if (nargin < 1 || nargin > 3)
+ usage ("x = dlmread (filename, sep, row)");
endif
- if nargin < 3, sep = ","; endif
+ if nargin < 2, sep = ","; endif
+ if nargin < 3, row = 0; endif
fid = fopen(filename, "r");
if (fid >= 0)
@@ -45,18 +47,28 @@
if (nr > 0)
in(idx) = " ";
nr += (idx(length(idx)) < length(in));
+ idxl = idx;
endif
idx = find (in == "\r");
if (nr == 0)
nr = length(idx);
if (nr > 0) nr += (idx(length(idx)) < length(in)); endif
+ idxl = idx;
endif
if (length (idx) > 0) in(idx) = " "; endif
+ nr = nr-row;
+ ## find where to start reading data
+ if (row > 0)
+ istr = idxl(row)+1;
+ else
+ istr = 1;
+ endif
+
## convert separators to spaces
idx = find (in == sep);
if (length(idx) > 0) in(idx) = " "; endif
- [x, n, err] = sscanf(in, "%g");
+ [x, n, err] = sscanf(in(istr:length(in)), "%g");
if (!isempty(err))
error(["dlmread: ", err]);
elseif (rem(n, nr) != 0)
|