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 : The scalar MATLAB string representing
33 %> the field separator used in the file.<br>
34 %> This property is either specified by
35 %> the user or is inferred from the
36 %> file to be read.
37 %>
38 sep = [];
39 %>
40 %> ncol : The scalar MATLAB integer representing
41 %> the number of columns identified in the file.
42 %>
43 ncol = 0;
44 %>
45 %> nrow : The scalar MATLAB integer representing
46 %> the number of rows identified in the file.
47 %>
48 nrow = 0;
49 %>
50 %> df : The scalar MATLAB table containing the
51 %> contents of the file as a dataframe.
52 %>
53 df = [];
54 end
55
56 methods(Access = public)
57
58 %> \brief
59 %> Construct and return a scalar object of class [pm.io.FileContentsTabular](@ref FileContentsTabular).
60 %>
61 %> \details
62 %> This is the constructor of the class [pm.io.FileContentsTabular](@ref FileContentsTabular).
63 %>
64 %> \param[in] file : See the documentation of the corresponding argument of [pm.io.FileContents](@ref FileContents).
65 %> \param[in] silent : See the documentation of the corresponding argument of [pm.io.FileContents](@ref FileContents).
66 %> \param[in] sep : The input scalar MATLAB string containing the field separator used in the file.<br>
67 %> (**optional**. The default is inferred from the contents of the specified input ``file``.)
68 %>
69 %> \return
70 %> ``self`` : The output scalar object of class [pm.io.FileContentsTabular](@ref FileContentsTabular).
71 %>
72 %> \interface{FileContentsTabular}
73 %> \code{.m}
74 %>
75 %> contents = pm.io.FileContentsTabular(file)
76 %> contents = pm.io.FileContentsTabular(file, [])
77 %> contents = pm.io.FileContentsTabular(file, silent)
78 %> contents = pm.io.FileContentsTabular(file, [], [])
79 %> contents = pm.io.FileContentsTabular(file, [], sep)
80 %> contents = pm.io.FileContentsTabular(file, silent, [])
81 %> contents = pm.io.FileContentsTabular(file, silent, sep)
82 %>
83 %> \endcode
84 %>
85 %> \final{FileContentsTabular}
86 %>
87 %> \author
88 %> \JoshuaOsborne, May 21 2024, 6:11 PM, University of Texas at Arlington<br>
89 %> \FatemehBagheri, May 20 2024, 1:25 PM, NASA Goddard Space Flight Center (GSFC), Washington, D.C.<br>
90 %> \AmirShahmoradi, May 16 2016, 9:03 AM, Oden Institute for Computational Engineering and Sciences (ICES), UT Austin<br>
91 function self = FileContentsTabular(file, silent, sep)
92 if nargin < 3
93 sep = [];
94 end
95 if nargin < 2
96 silent = [];
97 end
98 if nargin < 1
99 file = [];
100 end
101 self = self@pm.io.FileContents(file, silent);
102 if ~isfile(self.file)
103 error ( newline ...
104 + "The specified input file:" + newline ...
105 + newline ...
106 + pm.io.tab + """" + self.file + """" + newline ...
107 + newline ...
108 + "does not exist." + newline ...
109 + newline ...
110 );
111 end
112 if pm.array.len(sep) == 0
113 [d, self.sep, ~] = importdata(self.file);
114 elseif length(convertStringsToChars(sep)) == 1
115 [d, self.sep, ~] = importdata(self.file, sep);
116 else
117 weblinks = pm.lib.weblinks();
118 error ( newline ...
119 + "Multicharacter separators are unsupported as of ParaMonte 2." + newline ...
120 + "You have specified sep = """ + sep + """." + newline ...
121 + "If this feature is desired, please report this request at:" + newline ...
122 + newline ...
123 + pm.io.tab + pm.web.href(weblinks.github.issues.url) + newline ...
124 + newline ...
125 );
126 end
127 if isempty(d)
128 error ( newline ...
129 + "The specified input file is empty:" + newline ...
130 + newline ...
131 + pm.io.tab + self.file + newline ...
132 + newline ...
133 );
134 elseif ~isfield(d, "colheaders") || ~isfield(d, "data")
135 error ( newline ...
136 + "The specified input file is not a table:" + newline ...
137 + newline ...
138 + pm.io.tab + self.file + newline ...
139 + newline ...
140 + "The choice of field separator (""" + sep + """) may be incorrect." + newline ...
141 + newline ...
142 );
143 end
144 self.ncol = size(d.data, 2);
145 self.nrow = size(d.data, 1);
146 self.df = array2table(d.data, 'VariableNames', d.colheaders);
147 self.checkpoint();
148 if ~self.silent
149 disp("ncol = " + string(self.ncol) + ", nrow = " + string(self.nrow));
150 end
151 end
152
153 end
154
155end
function choice()
Return the ParaMonte-preferred MPI library vendor name as used in naming the ParaMonte MATLAB shared ...
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.