Skip to content

Commit 45f9021

Browse files
committed
readdbtable: new function
* INDEX: add ref to new function * inst/readdbtable.m: new file
1 parent fa8c7f4 commit 45f9021

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

INDEX

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ Database Operations
1515
Support Functions
1616
dbtable
1717
struct2dbtable
18+
readdbtable

inst/readdbtable.m

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
## Copyright (C) 2022 John Donoghue <john.donoghue@ieee.org>
2+
##
3+
## This program is free software: you can redistribute it and/or modify it
4+
## under the terms of the GNU General Public License as published by
5+
## the Free Software Foundation, either version 3 of the License, or
6+
## (at your option) any later version.
7+
##
8+
## This program is distributed in the hope that it will be useful, but
9+
## WITHOUT ANY WARRANTY; without even the implied warranty of
10+
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
## GNU General Public License for more details.
12+
##
13+
## You should have received a copy of the GNU General Public License
14+
## along with this program. If not, see
15+
## <https://www.gnu.org/licenses/>.
16+
17+
## -*- texinfo -*-
18+
## @deftypefn {} {@var{t} =} readdbtable (@var{filename})
19+
## Create a dbtable from a file
20+
##
21+
## Currently, this is using a very simplistic approach
22+
##
23+
## @subsubheading Inputs
24+
## @table @asis
25+
## @item @var{filename}
26+
## Filename for file containing tabular data
27+
## @end table
28+
##
29+
## @subsubheading Outputs
30+
## @table @asis
31+
## @item @var{t}
32+
## a dbtable of the read data
33+
## @end table
34+
##
35+
## @end deftypefn
36+
37+
function t = readdbtable (filename, varargin)
38+
if nargin < 0 || !ischar(filename)
39+
error ("Expected filename as a string");
40+
endif
41+
42+
[~, ~, ext] = fileparts(filename);
43+
44+
if isempty(ext)
45+
ext = "";
46+
endif
47+
48+
switch tolower(ext)
49+
case { ".txt" ".csv" ".dat" }
50+
format = "csv";
51+
otherwise
52+
format = "unknown";
53+
endswitch
54+
55+
if strcmp(format, "csv")
56+
t = read_dbtable_csv(filename);
57+
else
58+
error ("Unknown format for filename");
59+
endif
60+
61+
endfunction
62+
63+
%!error readbtable()
64+
65+
%!test
66+
%! testfile = [tempname ".csv"];
67+
%! fd = fopen(testfile, "w+t");
68+
%! unwind_protect
69+
%! fprintf(fd, "Column1,Column2,Column3\n");
70+
%! fprintf(fd, "Line1,1,2\n");
71+
%! fprintf(fd, "Line2,2,\n");
72+
%! fprintf(fd, "Line3,3\n");
73+
%! fprintf(fd, "Line4,4\n");
74+
%! fprintf(fd, "\n");
75+
%! fclose(fd);
76+
%! fd = -1;
77+
%!
78+
%! t = readdbtable(testfile);
79+
%! assert(istable(t));
80+
%! assert(size(t), [4 3]);
81+
%! assert(t.Properties.VariableNames, {'Column1', 'Column2', 'Column3'});
82+
%! unwind_protect_cleanup
83+
%! if fd >= 0
84+
%! fclose(fd);
85+
%! endif
86+
%! delete(testfile);
87+
%! end_unwind_protect
88+

0 commit comments

Comments
 (0)