ParaMonte MATLAB 3.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
FileContentsTabular.m
Go to the documentation of this file.
1%> \brief
2%> This is the base class for generating objects
3%> that contain the **tabular contents** of a given file.
4%>
5%> \details
6%> This class is meant to be primarily internally used
7%> by the ParaMonte library routines (e.g., samplers).
8%>
9%> Tabular file comprises a header line followed by a table of data.<br>
10%> If there are more than one header line, only the last such line is
11%> considered as header and the rest are discarded as text.<br>
12%>
13%> \note
14%> See the documentation of the class constructor.<br>
15%>
16%> \note
17%> See below for information on the attributes (properties).
18%>
19%> \note
20%> See below for information on the methods.
21%>
22%> \final{FileContentsTabular}
23%>
24%> \author
25%> \JoshuaOsborne, May 21 2024, 6:08 PM, University of Texas at Arlington<br>
26%> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
27%> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
28classdef FileContentsTabular < pm.io.FileContents
29
30 properties(Access = public)
31 %>
32 %> ``sep``
33 %>
34 %> The scalar MATLAB string representing
35 %> the field separator used in the file.<br>
36 %> This property is either specified by
37 %> the user or is inferred from the
38 %> file to be read.<br>
39 %>
40 sep = [];
41 %>
42 %> ``ncol``
43 %>
44 %> The scalar MATLAB integer representing
45 %> the number of columns identified in the file.<br>
46 %>
47 ncol = 0;
48 %>
49 %> ``nrow``
50 %>
51 %> The scalar MATLAB integer representing
52 %> the number of rows identified in the file.<br>
53 %>
54 nrow = 0;
55 %>
56 %> ``df``
57 %>
58 %> The scalar MATLAB ``table`` containing the
59 %> contents of the file as a dataframe.<br>
60 %>
61 df = [];
62 end
63
64 methods(Access = public)
65
66 %> \brief
67 %> Construct and return a scalar object of class [pm.io.FileContentsTabular](@ref FileContentsTabular).
68 %>
69 %> \details
70 %> This is the constructor of the class [pm.io.FileContentsTabular](@ref FileContentsTabular).
71 %>
72 %> \param[in] file : See the documentation of the corresponding argument of [pm.io.FileContents](@ref FileContents).
73 %> \param[in] silent : See the documentation of the corresponding argument of [pm.io.FileContents](@ref FileContents).
74 %> \param[in] sep : The input scalar MATLAB string containing the field separator used in the file.<br>
75 %> Use ``"\t"`` to represent the horizontal tab character.<br>
76 %> (**optional**. The default is inferred from the contents of the specified input ``file``.)
77 %>
78 %> \return
79 %> ``self`` : The output scalar object of class [pm.io.FileContentsTabular](@ref FileContentsTabular).
80 %>
81 %> \interface{FileContentsTabular}
82 %> \code{.m}
83 %>
84 %> contents = pm.io.FileContentsTabular(file)
85 %> contents = pm.io.FileContentsTabular(file, [])
86 %> contents = pm.io.FileContentsTabular(file, silent)
87 %> contents = pm.io.FileContentsTabular(file, [], [])
88 %> contents = pm.io.FileContentsTabular(file, [], sep)
89 %> contents = pm.io.FileContentsTabular(file, silent, [])
90 %> contents = pm.io.FileContentsTabular(file, silent, sep)
91 %>
92 %> \endcode
93 %>
94 %> \final{FileContentsTabular}
95 %>
96 %> \author
97 %> \JoshuaOsborne, May 21 2024, 6:11 PM, University of Texas at Arlington<br>
98 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
99 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
100 function self = FileContentsTabular(file, silent, sep)
101 if nargin < 3
102 sep = [];
103 end
104 if nargin < 2
105 silent = [];
106 end
107 if nargin < 1
108 file = [];
109 end
110 if pm.array.len(sep) == 0
111 sep = "";
112 else
113 sep = string(sep);
114 end
115 self = self@pm.io.FileContents(file, silent);
116 if ~isfile(self.file)
117 error ( newline ...
118 + "The specified input file:" + newline ...
119 + newline ...
120 + pm.io.tab + """" + self.file + """" + newline ...
121 + newline ...
122 + "does not exist." + newline ...
123 + newline ...
124 );
125 end
126 if pm.array.len(sep) == 0
127 [d, self.sep, ~] = importdata(self.file);
128 self.sep = string(self.sep);
129 elseif length(convertStringsToChars(sep)) == 1 || strcmpi(sep, "\t")
130 [d, self.sep, ~] = importdata(self.file, convertStringsToChars(sep));
131 self.sep = string(self.sep);
132 else
133 weblinks = pm.lib.weblinks();
134 error ( newline ...
135 + "Multicharacter separators are unsupported as of ParaMonte 2." + newline ...
136 + "You have specified sep = """ + sep + """." + newline ...
137 + "If this feature is desired, please report this request at:" + newline ...
138 + newline ...
139 + pm.io.tab + pm.web.href(weblinks.github.issues.url) + newline ...
140 + newline ...
141 );
142 end
143 if isempty(d)
144 error ( newline ...
145 + "The specified input file is empty:" + newline ...
146 + newline ...
147 + pm.io.tab + self.file + newline ...
148 + newline ...
149 );
150 elseif ~isfield(d, "colheaders") || ~isfield(d, "data")
151 error ( newline ...
152 + "The specified input file is not a table:" + newline ...
153 + newline ...
154 + pm.io.tab + self.file + newline ...
155 + newline ...
156 + "The default or the inferred choice of field separator (""" + sep + """) may be incorrect." + newline ...
157 + "Alternatively, the specified file contents may be corrupted and non-tabular." + newline ...
158 + "If the specified tile path is a weblink, ensure the weblink corrently points" + newline ...
159 + "to the target raw tabular ASCII file and not an HTML page." + newline ...
160 + newline ...
161 );
162 end
163 self.ncol = size(d.data, 2);
164 self.nrow = size(d.data, 1);
165 self.df = array2table(d.data, 'VariableNames', d.colheaders);
166 self.checkpoint();
167 if ~self.silent
168 disp("ncol = " + string(self.ncol) + ", nrow = " + string(self.nrow));
169 end
170 end
171
172 end
173
174end
This is the base class for generating objects that contain the tabular contents of a given file.
function FileContentsTabular(in file, in silent, in sep)
Construct and return a scalar object of class pm.io.FileContentsTabular.
This is the base class for generating objects that contain the contents of a given file.
Definition: FileContents.m:27
function href(in url)
Return an HTML-style decoration of the input URL if the ParaMonte MATLAB library is used in GUI,...
function len(in obj)
Return a scalar MATLAB whole-number containing the length of the input scalar or vector object.
function lib()
Return a scalar MATLAB string containing the path to the lib directory of the ParaMonte library packa...
function tab()
Return a scalar MATLAB string containing 4 blank characters equivalent to a tab character.