time | Calls | line |
---|
< 0.001 | 2 | 1 | classdef SpreadsheetInputs < matlab.io.internal.FunctionInterface &...
|
| | 2 | matlab.io.internal.functions.AcceptsSheetNameOrNumber
|
| | 3 | %SHAREDSPREADSHEETINPUTS Inputs common to detection, and ImportOptions
|
| | 4 | %properties.
|
| | 5 |
|
| | 6 | % Copyright 2018 The MathWorks, Inc.
|
| | 7 |
|
| | 8 | properties (Parameter)
|
| | 9 | %DATARANGE where to find the data in a spreadsheet file
|
| | 10 | % The DATARANGE is where the data values are found.
|
| | 11 | %
|
| | 12 | % Can be any of the following Ranges:
|
| | 13 | %
|
| | 14 | % cell: The starting cell for the data. The total
|
| | 15 | % range will extend to the last empty row or
|
| | 16 | % until reaching the footer Range, and will
|
| | 17 | % contain 1 column for each variable.
|
| | 18 | %
|
| | 19 | % range: The exact range to read. The number of
|
| | 20 | % columns must match the number of variables.
|
| | 21 | % Data will only be read from the rows
|
| | 22 | % specified. Empty cells will be imported as
|
| | 23 | % missing cells.
|
| | 24 | %
|
| | 25 | % row-range: Selection of rows to read. The data will be
|
| | 26 | % read from the first non-empty column and
|
| | 27 | % one column for each variable.
|
| | 28 | %
|
| | 29 | % column-range: Selection of columns to read. There must be
|
| | 30 | % the same number of columns as import
|
| | 31 | % variables. The first record will be read
|
| | 32 | % from the first non-empty row.
|
| | 33 | %
|
| | 34 | % number-index: The first row which contains data. Data
|
| | 35 | % will be read until the end of the file, or
|
| | 36 | % the footer Range.
|
| | 37 | %
|
| | 38 | % set of row-ranges: A set of row-ranges to read expressed as a
|
| | 39 | % cell array or string array of row-ranges.
|
| | 40 | % The data will be read from each row-range
|
| | 41 | % and vertically concatenated. Example:
|
| | 42 | % {'1:4';'7:12'} or ["3:10";"15:20"]
|
| | 43 | %
|
| | 44 | % set of row-intervals: A set of row-intervals to read. Same as
|
| | 45 | % "set of row-ranges" except specified as a
|
| | 46 | % numeric array of intervals. Example:
|
| | 47 | % [1,2;5,8]
|
| | 48 | %
|
| | 49 | % empty: No data will be read.
|
| | 50 | %
|
| | 51 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 52 | DataRange = 'A1';
|
| | 53 |
|
| | 54 | %VARIABLEUNITSRANGE Range of Variable Units in the spreadsheet
|
| | 55 | % VARIABLEUNITSRANGE can contain only 1 row or column, or be empty.
|
| | 56 | %
|
| | 57 | % Can be any of the following Ranges:
|
| | 58 | % cell: The starting cell for the units. The range will extend to
|
| | 59 | % contain 1 column for each variable.
|
| | 60 | %
|
| | 61 | % range: The exact range to read. The number of columns must match
|
| | 62 | % the number of variables and contain only one row.
|
| | 63 | %
|
| | 64 | % row-range: Selection of rows to read. Must be a single row.
|
| | 65 | %
|
| | 66 | % number-index: The row which contains units.
|
| | 67 | %
|
| | 68 | % empty: Indicates there are no units.
|
| | 69 | %
|
| | 70 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 71 | % matlab.io.spreadsheet.SpreadsheetImportOptions/DataRange
|
| | 72 | VariableUnitsRange = '';
|
| | 73 |
|
| | 74 | %VARIABLENAMESRANGE Range of Variable names in the spreadsheet
|
| | 75 | % VARIABLENAMESRANGE can contain only 1 row or column, or be empty.
|
| | 76 | %
|
| | 77 | % Can be any of the following Ranges:
|
| | 78 | % cell: The starting cell for the variable names. The range will
|
| | 79 | % extend to contain 1 column for each variable.
|
| | 80 | %
|
| | 81 | % range: The exact range to read. The number of columns must match
|
| | 82 | % the number of variables and contain only one row.
|
| | 83 | %
|
| | 84 | % row-range: Must be a single row.
|
| | 85 | %
|
| | 86 | % number-index: The row which contains variable names.
|
| | 87 | %
|
| | 88 | % empty: Indicates there are no variable names.
|
| | 89 | %
|
| | 90 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 91 | % matlab.io.spreadsheet.SpreadsheetImportOptions/DataRange
|
| | 92 | VariableNamesRange = '';
|
| | 93 |
|
| | 94 | %VARIABLEDESCRIPTIONSRANGE Range of variable descriptions in the spreadsheet
|
| | 95 | % VARIABLEDESCRIPTIONSRANGE Where variable descriptions are found.
|
| | 96 | % Variable descriptions must have the same number of columns as
|
| | 97 | % variables.
|
| | 98 | %
|
| | 99 | % Can be any of the following Ranges:
|
| | 100 | % range: The exact range to read. The number of columns must match
|
| | 101 | % the number of variables.
|
| | 102 | %
|
| | 103 | % row-range: Must be a single row.
|
| | 104 | %
|
| | 105 | % empty: Indicates there are no variable descriptions.
|
| | 106 | %
|
| | 107 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 108 | % matlab.io.spreadsheet.SpreadsheetImportOptions/DataRange
|
| | 109 | VariableDescriptionsRange = '';
|
| | 110 |
|
| | 111 | %ROWNAMESRANGE Range of row names in the spreadsheet
|
| | 112 | % ROWNAMESRANGE can contain only 1 column or row, or be empty.
|
| | 113 | %
|
| | 114 | % Can be any of the following Ranges:
|
| | 115 | % cell: The starting cell for the row names. A row name will be
|
| | 116 | % read for each data row.
|
| | 117 | %
|
| | 118 | % range: The exact range to read. The number of rows must match
|
| | 119 | % the number of datarows and contain only one column.
|
| | 120 | %
|
| | 121 | % column-range: Must be a single column.
|
| | 122 | %
|
| | 123 | % number-index: The column which contains row names.
|
| | 124 | %
|
| | 125 | % empty: Indicates there are no row names.
|
| | 126 | %
|
| | 127 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 128 | % matlab.io.spreadsheet.SpreadsheetImportOptions/DataRange
|
| | 129 | RowNamesRange = '';
|
| | 130 |
|
| | 131 | end
|
| | 132 |
|
| | 133 | methods % get/set
|
| | 134 | function obj = set.DataRange(obj,rhs)
|
| | 135 | try
|
| | 136 | obj.DataRange = obj.validateDataRange(rhs);
|
| | 137 | catch ME
|
| | 138 | throwAsCaller(ME);
|
| | 139 | end
|
| | 140 | end
|
| | 141 |
|
| | 142 | function obj = set.VariableNamesRange(obj,rhs)
|
| | 143 | try
|
| | 144 | obj.VariableNamesRange = validateRange(rhs,'VariableNamesRange',@SingleRecordNumVars,obj.getNumVars());
|
| | 145 | catch ME
|
| | 146 | throwAsCaller(ME);
|
| | 147 | end
|
| | 148 | end
|
| | 149 |
|
| | 150 | function obj = set.RowNamesRange(obj,rhs)
|
| | 151 | try
|
| | 152 | obj.RowNamesRange = validateRange(rhs,'RowNamesRange',@SingleVariable,obj.getNumVars());
|
| | 153 | catch ME
|
| | 154 | throwAsCaller(ME);
|
| | 155 | end
|
| | 156 | end
|
| | 157 |
|
| | 158 | function obj = set.VariableUnitsRange(obj,rhs)
|
| | 159 | try
|
| | 160 | obj.VariableUnitsRange = validateRange(rhs,'VariableUnitsRange',@SingleRecordNumVars,obj.getNumVars());
|
| | 161 | catch ME
|
| | 162 | throwAsCaller(ME);
|
| | 163 | end
|
| | 164 | end
|
| | 165 |
|
| | 166 | function obj = set.VariableDescriptionsRange(obj,rhs)
|
| | 167 | try
|
| | 168 | obj.VariableDescriptionsRange = validateRange(rhs,'VariableDescriptionsRange',@SingleRecordNumVars,obj.getNumVars());
|
| | 169 | catch ME
|
| | 170 | throwAsCaller(ME);
|
| | 171 | end
|
| | 172 | end
|
| | 173 | end
|
| | 174 |
|
| | 175 | methods(Access = protected)
|
| | 176 | function [rhs,obj] = setSheet(obj,rhs)
|
| | 177 | rhs = convertStringsToChars(rhs);
|
| | 178 | if isnumeric(rhs) && isscalar(rhs) && rhs > 0 && floor(rhs)==rhs
|
| | 179 | % nothing needed
|
| | 180 | elseif ischar(rhs) && (isrow(rhs) || isempty(rhs))
|
| | 181 | n = numel(rhs);
|
| | 182 | if n < 0 || n > 31 || any(ismember(rhs,':\/?*[]'))
|
| | 183 | error(message('MATLAB:spreadsheet:book:invalidSheetName'));
|
| | 184 | end
|
| | 185 | else
|
| | 186 | error(message('MATLAB:spreadsheet:importoptions:BadSheet'));
|
| | 187 | end
|
| | 188 |
|
| | 189 | if isempty(rhs)
|
| | 190 | rhs = '';
|
| | 191 | end
|
| | 192 | end
|
| | 193 | end
|
| | 194 |
|
| | 195 | methods (Abstract)
|
| | 196 | n = getNumVars(obj);
|
| | 197 | end
|
| | 198 |
|
| | 199 | methods (Access = protected)
|
| | 200 | function rhs = validateDataRange(obj,rhs)
|
| | 201 | % Determine how DataRange was specified and then delegate to
|
| | 202 | % the appropriate validators
|
| | 203 | rhs = convertStringsToChars(rhs);
|
| | 204 | [nrows, ncols] = size(rhs);
|
| | 205 |
|
| | 206 | if (~ischar(rhs) && ncols > 1) || nrows > 1
|
| | 207 | temprhs = rhs;
|
| | 208 | if iscellstr(rhs)
|
| | 209 | for i = 1:nrows
|
| | 210 | type = ...
|
| | 211 | matlab.io.spreadsheet.internal.validateRange(rhs{i});
|
| | 212 | if ~strcmp(type, 'row-only')
|
| | 213 | error(message('MATLAB:spreadsheet:importoptions:RowOnlyRanges'));
|
| | 214 | end
|
| | 215 | end
|
| | 216 | % convert the intervals to numeric for validation
|
| | 217 | temprhs = str2double(split(string(rhs),':'));
|
| | 218 | elseif ~isnumeric(rhs)
|
| | 219 | error(message('MATLAB:spreadsheet:importoptions:InvalidDataRange'));
|
| | 220 | else
|
| | 221 | % Convert to double
|
| | 222 | rhs = double(rhs);
|
| | 223 | end
|
| | 224 | try
|
| | 225 | % validate the 'DataLines' syntax
|
| | 226 | matlab.io.internal.validators.validateLineIntervals(temprhs,'DataRange');
|
| | 227 | catch ME
|
| | 228 | if strcmp(ME.identifier,'MATLAB:textio:io:InvalidDataLines')
|
| | 229 | error(message('MATLAB:spreadsheet:importoptions:InvalidDataRange'));
|
| | 230 | else
|
| | 231 | throw(ME);
|
| | 232 | end
|
| | 233 | end
|
| | 234 | elseif isempty(rhs)
|
| | 235 | error(message('MATLAB:spreadsheet:importoptions:InvalidDataRange'));
|
| | 236 | else
|
| | 237 | rhs = validateRange(rhs,'DataRange',@NumVars,obj.getNumVars());
|
| | 238 | end
|
| | 239 | end
|
| | 240 | end
|
| | 241 | end
|
| | 242 |
|
| | 243 | function rhs = validateRange(rhs,propname,orientationValidator,numElems)
|
| | 244 | rhs = convertStringsToChars(rhs);
|
| | 245 | if ischar(rhs)
|
| | 246 | rhs = strtrim(rhs);
|
| | 247 | end
|
| | 248 | if isempty(rhs)
|
| | 249 | rhs = '';
|
| | 250 | return;
|
| | 251 | end
|
| | 252 | try
|
| | 253 | if isscalar(rhs) && isnumeric(rhs)
|
| | 254 | % Scalar is supported, number of row/column
|
| | 255 | if any(floor(rhs)~=rhs) || (rhs <= 0) || isinf(rhs)
|
| | 256 | error(message('MATLAB:spreadsheet:importoptions:InvalidScalarLocation',propname));
|
| | 257 | end
|
| | 258 | % Convert to double
|
| | 259 | rhs = double(rhs);
|
| | 260 | else
|
| | 261 | % Either four element vector or range string.
|
| | 262 | [type, rangesize] = ...
|
| | 263 | matlab.io.spreadsheet.internal.validateRange(rhs);
|
| | 264 | orientationValidator(propname,type,rangesize,numElems);
|
| | 265 | end
|
| | 266 | catch ME
|
| | 267 | if propname=="DataRange" && ismember(ME.identifier,{'MATLAB:spreadsheet:sheet:invalidRangeSpec',...
|
| | 268 | 'MATLAB:spreadsheet:sheet:rangeParseInvalid'})
|
| | 269 | error(message('MATLAB:spreadsheet:importoptions:InvalidDataRange'));
|
| | 270 | end
|
| | 271 | throwAsCaller(ME)
|
| | 272 | end
|
| | 273 | end
|
| | 274 |
|
| | 275 | function NumVars(propname,type,rangesize,nvars)
|
| | 276 | % must match the number of variables, be a start cell, be open-ended (cell/row), or a named range.
|
| | 277 | if nvars < inf && ~any(rangesize(2) == [nvars -1]) && ~strcmp(type,'single-cell')
|
| | 278 | error(message('MATLAB:spreadsheet:importoptions:VarNumberMismatch',propname));
|
| | 279 | end
|
| | 280 | end
|
| | 281 |
|
| | 282 | function SingleRecordNumVars(propname,type,rangesize,nvars)
|
| | 283 | % Allowed:
|
| | 284 | % * A single open record range
|
| | 285 | % * The number of variables must match, or be open.
|
| | 286 | % * An exact range.
|
| | 287 |
|
| | 288 | if strcmp(type,'column-only')
|
| | 289 | error(message('MATLAB:spreadsheet:importoptions:PropSupportOpenCol',propname));
|
| | 290 | end
|
| | 291 | % must be a single row, or a named range.
|
| | 292 | if ~any(rangesize(1) == [-1 1])
|
| | 293 | error(message('MATLAB:spreadsheet:importoptions:SingleRow',propname));
|
| | 294 | end
|
| | 295 | % must match the number of variables, be a start cell, be open-ended (cell/row), or a named range.
|
| | 296 |
|
| | 297 | if nvars < inf && ~any(rangesize(2) == [nvars 1 -1])
|
| | 298 | error(message('MATLAB:spreadsheet:importoptions:VarNumberMismatch',propname));
|
| | 299 | end
|
| | 300 |
|
| | 301 | end
|
| | 302 |
|
| | 303 | function SingleVariable(propname,type,rangesize,~)
|
| | 304 | % Cannot support rows
|
| | 305 | if strcmp(type,'row-only')
|
| | 306 | error(message('MATLAB:spreadsheet:importoptions:PropSupportOpenRow',propname));
|
| | 307 | end
|
| | 308 | % must match the number of variables, be open-ended (cell/row), or a named range.
|
| | 309 | if ~any(rangesize(2) == [-1 1])
|
| | 310 | error(message('MATLAB:spreadsheet:importoptions:SingleCol',propname));
|
| | 311 | end
|
| | 312 | end
|
| | 313 |
|