52 if (is.peek () == std::istream::traits_type::eof ())
56 if (::isalpha (is.peek ()))
59 while (is && ::isalpha (is.peek ()))
95 if (ch ==
'.' || ch ==
':')
115 if (ch ==
'.' || ch ==
':')
121 if (!is || ch !=
'.')
132 if (!is || !is.eof ())
138 if (stat && is && !is.eof ())
141 if (!is || !is.eof ())
159 DEFUN (dlmread, args, ,
161 @deftypefn {Built-in Function} {@var{data} =} dlmread (@var{file})\n\
162 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@var{file}, @var{sep})\n\
163 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{r0}, @var{c0})\n\
164 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@var{file}, @var{sep}, @var{range})\n\
165 @deftypefnx {Built-in Function} {@var{data} =} dlmread (@dots{}, \"emptyvalue\", @var{EMPTYVAL})\n\
166 Read the matrix @var{data} from a text file which uses the delimiter\n\
167 @var{sep} between data values.\n\
169 If @var{sep} is not defined the separator between fields is determined from\n\
172 Given two scalar arguments @var{r0} and @var{c0}, these define the starting\n\
173 row and column of the data to be read. These values are indexed from zero,\n\
174 such that the first row corresponds to an index of zero.\n\
176 The @var{range} parameter may be a 4-element vector containing the upper\n\
177 left and lower right corner @code{[@var{R0},@var{C0},@var{R1},@var{C1}]}\n\
178 where the lowest index value is zero. Alternatively, a spreadsheet style\n\
179 range such as @qcode{\"A2..Q15\"} or @qcode{\"T1:AA5\"} can be used. The\n\
180 lowest alphabetical index @qcode{'A'} refers to the first column. The\n\
181 lowest row index is 1.\n\
183 @var{file} should be a file name or file id given by @code{fopen}. In the\n\
184 latter case, the file is read until end of file is reached.\n\
186 The @qcode{\"emptyvalue\"} option may be used to specify the value used to\n\
187 fill empty fields. The default is zero.\n\
188 @seealso{csvread, textscan, textread, dlmwrite}\n\
193 int nargin = args.
length ();
195 double empty_value = 0.0;
197 if (nargin > 2 && args(nargin-2).is_string ()
198 && args(nargin-2).string_value () ==
"emptyvalue")
200 empty_value = args(nargin-1).double_value ();
206 if (nargin < 1 || nargin > 4)
212 std::istream *
input = 0;
213 std::ifstream input_file;
215 if (args(0).is_string ())
218 std::string fname (args(0).string_value ());
222 input_file.open (tname.c_str (), std::ios::in);
226 error (
"dlmread: unable to open file '%s'", fname.c_str ());
232 else if (args(0).is_scalar_type ())
243 error (
"dlmread: stream FILE not open for input");
249 error (
"dlmread: FILE argument must be a string or file id");
257 if (args(1).is_sq_string ())
260 sep = args(1).string_value ();
276 error (
"dlmread: error parsing RANGE");
278 else if (nargin == 4)
280 r0 = args(2).idx_type_value ();
281 c0 = args(3).idx_type_value ();
287 if (r0 < 0 || c0 < 0)
288 error (
"dlmread: left & top must be positive");
303 bool iscmplx =
false;
304 bool sepflag =
false;
310 getline (*input, line);
313 std::istringstream tmp_stream;
317 while (getline (*input, line))
320 if (line.find_first_not_of (
" \t") == std::string::npos)
327 size_t n = line.find_first_of (
",:; \t",
328 line.find_first_of (
"0123456789"));
329 if (n == std::string::npos)
336 char ch = line.at (n);
357 size_t pos1 = line.find_first_not_of (
" \t");
360 size_t pos2 = line.find_first_of (sep, pos1);
362 if (sepflag && pos2 != std::string::npos)
365 pos2 = line.find_first_not_of (sep, pos2);
366 if (pos2 != std::string::npos)
369 pos2 = line.length () - 1;
374 if (pos2 != std::string::npos)
377 pos1 = std::string::npos;
380 while (pos1 != std::string::npos);
383 cdata.
resize (rmax, cmax);
385 rdata.
resize (rmax, cmax);
388 r = (r > i + 1 ? r : i + 1);
391 size_t pos1 = line.find_first_not_of (
" \t");
396 size_t pos2 = line.find_first_of (sep, pos1);
397 std::string str = line.substr (pos1, pos2 - pos1);
399 if (sepflag && pos2 != std::string::npos)
401 pos2 = line.find_first_not_of (sep, pos2) - 1;
403 c = (c > j + 1 ? c : j + 1);
404 if (r > rmax || c > cmax)
411 cdata.
resize (rmax, cmax);
413 rdata.
resize (rmax, cmax);
416 tmp_stream.str (str);
422 if (tmp_stream.eof ())
429 else if (std::toupper (tmp_stream.peek ()) ==
'I')
441 if (!iscmplx && y != 0.)
454 cdata(i,j++) = empty_value;
456 rdata(i,j++) = empty_value;
458 if (pos2 != std::string::npos)
461 pos1 = std::string::npos;
464 while (pos1 != std::string::npos);
481 cdata = cdata.
extract (0, c0, r1, c1);
483 rdata = rdata.
extract (0, c0, r1, c1);
static octave_stream lookup(int fid, const std::string &who=std::string())
double octave_read_double(std::istream &is)
static bool parse_range_spec(const octave_value &range_spec, octave_idx_type &rlo, octave_idx_type &clo, octave_idx_type &rup, octave_idx_type &cup)
void resize(octave_idx_type nr, octave_idx_type nc, double rfv=0)
Matrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
OCTINTERP_API void print_usage(void)
octave_idx_type length(void) const
friend class ComplexMatrix
#define DEFUN(name, args_name, nargout_name, doc)
void error(const char *fmt,...)
RowVector row(octave_idx_type i) const
std::istream * input_stream(void)
static std::string tilde_expand(const std::string &)
octave_idx_type numel(const octave_value_list &idx)
std::string string_value(bool force=false) const
bool is_string(void) const
static const octave_idx_type idx_max
charNDArray max(char d, const charNDArray &m)
void resize(octave_idx_type nr, octave_idx_type nc, const Complex &rfv=Complex(0))
subroutine stat(x, n, av, var, xmin, xmax)
static bool read_cell_spec(std::istream &is, octave_idx_type &row, octave_idx_type &col)
Array< double > vector_value(bool frc_str_conv=false, bool frc_vec_conv=false) const
std::string do_string_escapes(const std::string &s)
static int input(yyscan_t yyscanner)
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type octave_idx_type octave_idx_type c1
static MArray< double > const octave_idx_type const octave_idx_type octave_idx_type r1
std::complex< double > Complex
ComplexMatrix extract(octave_idx_type r1, octave_idx_type c1, octave_idx_type r2, octave_idx_type c2) const
F77_RET_T const double * x
bool is_real_matrix(void) const