time | Calls | line |
---|
< 0.001 | 2 | 1 | classdef (AllowedSubclasses = {?matlab.io.text.TextImportOptions,?matlab.io.spreadsheet.SpreadsheetImportOptions,?matlab.io.xml.XMLImportOptions}) ImportOptions < matlab.io.internal.mixin.HasPropertiesAsNVPairs...
|
| | 2 | & matlab.mixin.internal.Scalar ...
|
| | 3 | & matlab.io.internal.shared.MissingErrorRulesInputs ...
|
| | 4 | & matlab.io.internal.shared.PreserveVariableNamesInput ...
|
| | 5 | & matlab.io.internal.shared.save.Saveable
|
| | 6 | %
|
| | 7 |
|
| | 8 | % Copyright 2016-2019 The MathWorks, Inc.
|
| | 9 |
|
| | 10 | properties (Dependent)
|
| | 11 | %SELECTEDVARIABLENAMES Names of variables of interest
|
| | 12 | % By default, SelectedVariableNames is equal to VariableNames. It can be
|
| | 13 | % set to any unique subset of the VariableNames to indicate which
|
| | 14 | % variables should be imported.
|
| | 15 | %
|
| | 16 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 17 | SelectedVariableNames
|
| | 18 |
|
| | 19 | %VARIABLENAMES Names of variables
|
| | 20 | % The names to use when importing variables. If empty, variable names
|
| | 21 | % will be read from data, or generated as Var1, Var2, etc..
|
| | 22 | %
|
| | 23 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 24 | VariableNames
|
| | 25 |
|
| | 26 | %VARIABLEOPTIONS Options for each import variable
|
| | 27 | % VariableOptions is an array of VariableImportOptions of the same size
|
| | 28 | % as variable names. Each element of the array sets options for a
|
| | 29 | % specific variable.
|
| | 30 | %
|
| | 31 | % Example, setting an option for a variable by name:
|
| | 32 | % opts = detectImportOptions('patients.xls')
|
| | 33 | % opts = setvaropts(opts,'Gender','MissingRule','error');
|
| | 34 | %
|
| | 35 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 36 | % matlab.io.spreadsheet.SpreadsheetImportOptions/setvaropts
|
| | 37 | % matlab.io.spreadsheet.SpreadsheetImportOptions/getvaropts
|
| | 38 | % matlab.io.VariableImportOptions
|
| | 39 | VariableOptions
|
| | 40 |
|
| | 41 | %VARIABLETYPES Output types of the variables
|
| | 42 | % VariableTypes is a cell array of character vectors whose values
|
| | 43 | % indicate the datatype of the variable.
|
| | 44 | % The following types (and resulting import variables) are supported:
|
| | 45 | % * char - a cell array of character vectors
|
| | 46 | % * double - a double precision floating point number array
|
| | 47 | % * single - a single precision floating point number array
|
| | 48 | % * datetime - a datetime array
|
| | 49 | % * duration - a duration array
|
| | 50 | % * categorical - a categorical array
|
| | 51 | % * int8 - an 8-bit integer array
|
| | 52 | % * int16 - a 16-bit integer array
|
| | 53 | % * int32 - a 32-bit integer array
|
| | 54 | % * int64 - a 64-bit integer array
|
| | 55 | % * uint8 - an unsigned 8-bit integer array
|
| | 56 | % * uint16 - an unsigned 16-bit integer array
|
| | 57 | % * uint32 - an unsigned 32-bit integer array
|
| | 58 | % * uint64 - an unsigned 64-bit integer array
|
| | 59 | % * logical - a logical array
|
| | 60 | %
|
| | 61 | % See also matlab.io.spreadsheet.SpreadsheetImportOptions
|
| | 62 | % matlab.io.VariableImportOptions
|
| | 63 | % matlab.io.TextVariableImportOptions
|
| | 64 | % matlab.io.NumericVariableImportOptions
|
| | 65 | % matlab.io.DatetimeVariableImportOptions
|
| | 66 | % matlab.io.DurationVariableImportOptions
|
| | 67 | % matlab.io.CategoricalVariableImportOptions
|
| | 68 | VariableTypes
|
| | 69 | end
|
| | 70 |
|
| | 71 | properties (Access = private)
|
| | 72 | selected_vars = ':';
|
| | 73 | is_unbounded_selection(1,1) = true;
|
| | 74 | using_generated_names(1,1) = true;
|
| | 75 | var_opts = [];
|
| | 76 | end
|
| | 77 |
|
| | 78 | properties (Dependent, Access = ...
|
| | 79 | {?matlab.io.internal.functions.DetectImportOptionsText,...
|
| | 80 | ?matlab.io.internal.functions.DetectImportOptionsSpreadsheet,...
|
| | 81 | ?matlab.io.internal.functions.ReadMatrixWithImportOptions,...
|
| | 82 | ?matlab.io.internal.functions.ReadTimeTableWithImportOptions,...
|
| | 83 | ?matlab.io.internal.functions.ReadTableWithImportOptionsText,...
|
| | 84 | ?matlab.io.internal.functions.ReadTableWithImportOptionsSpreadsheet,...
|
| | 85 | ?matlab.io.internal.functions.ReadTableWithImportOptionsXML,...
|
| | 86 | ?matlab.io.internal.functions.SetVarOpts,...
|
| | 87 | ?matlab.io.ImportOptions,...
|
| | 88 | ?matlab.io.internal.readers.TextDataReader,...
|
| | 89 | ?matlab.io.internal.builders.Builder})
|
| | 90 | selectedIDs;
|
| | 91 | end
|
| | 92 |
|
| | 93 | properties (Access = ...
|
| | 94 | {?matlab.io.internal.functions.DetectImportOptionsText,...
|
| | 95 | ?matlab.io.internal.functions.DetectImportOptionsSpreadsheet,...
|
| | 96 | ?matlab.io.internal.functions.ReadMatrixWithImportOptions,...
|
| | 97 | ?matlab.io.internal.functions.ReadTimeTableWithImportOptions,...
|
| | 98 | ?matlab.io.internal.functions.ReadTableWithImportOptionsText,...
|
| | 99 | ?matlab.io.internal.functions.ReadTableWithImportOptionsSpreadsheet,...
|
| | 100 | ?matlab.io.internal.functions.SetVarOpts,...
|
| | 101 | ?matlab.io.ImportOptions,...
|
| | 102 | ?matlab.io.internal.readers.TextDataReader,...
|
| | 103 | ?matlab.io.internal.builders.Builder})
|
| | 104 | fast_var_opts matlab.io.internal.FastVarOpts = matlab.io.internal.FastVarOpts(1);
|
| | 105 | end
|
| | 106 |
|
| | 107 | properties (Hidden)
|
| | 108 | %CREATEDBY Hidden variable to check for value of the
|
| | 109 | % 'EmptyColumnType' field to determine what type to use when
|
| | 110 | % returning empty columns. For backward comptability purposes,
|
| | 111 | % currently we are passing in char for detectImportOptions, and
|
| | 112 | % double for readtable/datastore/spreadsheet.
|
| | 113 | EmptyColumnType char
|
| | 114 | end
|
| | 115 |
|
| | 116 | methods
|
| | 117 | function val = get.selectedIDs(obj)
|
| | 118 | if isequal(obj.selected_vars,':')
|
| | 119 | val = 1:obj.fast_var_opts.numVars();
|
| | 120 | else
|
| | 121 | val = obj.selected_vars;
|
| | 122 | end
|
| | 123 | end
|
| | 124 |
|
| | 125 | function obj = set.VariableTypes(obj,types)
|
| | 126 | vopts = obj.fast_var_opts;
|
| | 127 | n = vopts.numVars;
|
| | 128 | if ischar(types), types = {types}; end
|
| | 129 | if numel(types) ~= n
|
| | 130 | error(message('MATLAB:textio:io:ExpectedCellStrSize',n))
|
| | 131 | end
|
| | 132 | obj.fast_var_opts = vopts.setTypes(1:n,types(:));
|
| | 133 | end
|
| | 134 |
|
| | 135 | function obj = set.VariableOptions(obj,rhs)
|
| | 136 | if ~isa(rhs,'matlab.io.VariableImportOptions') || (~isvector(rhs) && ~isempty(rhs))
|
| | 137 | error(message('MATLAB:textio:io:ExpectedVarImportOpts'))
|
| | 138 | end
|
| | 139 | % replace empty names with Var##
|
| | 140 | nNew = numel(rhs);
|
| | 141 | nOld = obj.fast_var_opts.numVars;
|
| | 142 |
|
| | 143 | obj.fast_var_opts = matlab.io.internal.FastVarOpts.fromFullVarOpts(rhs);
|
| | 144 |
|
| | 145 | if nOld ~= nNew
|
| | 146 | obj = updatePerVarSizes(obj,nNew);
|
| | 147 | end
|
| | 148 | if obj.is_unbounded_selection && ~isequal(obj.selected_vars,':')
|
| | 149 | obj.selected_vars = [obj.selected_vars nOld+1:nNew];
|
| | 150 | end
|
| | 151 | end
|
| | 152 |
|
| | 153 | function obj = set.SelectedVariableNames(obj,rhs)
|
| | 154 | if (iscell(rhs) && ~iscellstr(rhs)) %#ok<ISCLSTR>
|
| | 155 | error(message('MATLAB:textio:io:BadSelectionInput'));
|
| | 156 | else
|
| | 157 | rhs = matlab.io.internal.validators.validateCellStringInput(rhs,'Selected Variable Names');
|
| | 158 | end
|
| | 159 | if isnumeric(rhs)
|
| | 160 | n = obj.fast_var_opts.numVars;
|
| | 161 | if ~all(rhs >= 1 & rhs <= n) || ~all(floor(rhs)==rhs)
|
| | 162 | error(message('MATLAB:textio:io:BadNumericSelection'));
|
| | 163 | end
|
| | 164 | elseif ischar(rhs) || iscell(rhs)
|
| | 165 | try rhs = cellstr(rhs);catch
|
| | 166 | error(message('MATLAB:textio:textio:InvalidStringOrCellStringProperty','SelectedVariableNames'));
|
| | 167 | end
|
| | 168 | rhs = getNumericSelection(obj,rhs);
|
| | 169 | else
|
| | 170 | error(message('MATLAB:textio:io:BadSelectionInput'))
|
| | 171 | end
|
| | 172 | obj.selected_vars = unique(rhs,'stable');
|
| | 173 | obj.is_unbounded_selection = isequal(rhs,':');
|
| | 174 | end
|
| | 175 |
|
| | 176 | function val = get.VariableNames(obj)
|
| | 177 | % TODO change this so, we don't have to do the transpose
|
| | 178 | val = obj.fast_var_opts.Names';
|
| | 179 | end
|
| | 180 |
|
| | 181 | function val = get.VariableTypes(obj)
|
| | 182 | % TODO change this so, we don't have to do the transpose
|
| | 183 | val = obj.fast_var_opts.Types';
|
| | 184 | end
|
| | 185 |
|
| | 186 | function val = get.VariableOptions(obj)
|
| | 187 | val = obj.fast_var_opts.getVarOpts();
|
| | 188 | end
|
| | 189 |
|
| | 190 | function val = get.SelectedVariableNames(obj)
|
| | 191 | val = obj.fast_var_opts.Names(obj.selected_vars,:)';
|
| | 192 | end
|
| | 193 |
|
| | 194 | function obj = set.VariableNames(obj,newNames)
|
| | 195 | newNames = convertStringsToChars(newNames);
|
| | 196 | if ischar(newNames)
|
| | 197 | newNames = {newNames};
|
| | 198 | end
|
| | 199 | newNames = newNames(:);
|
| | 200 |
|
| | 201 | import matlab.io.internal.validators.validateVariableName;
|
| | 202 | validateFcn = @(name) validateVariableName(name, false);
|
| | 203 |
|
| | 204 | if ~iscell(newNames) || ~all(cellfun(validateFcn,newNames))
|
| | 205 | error(message('MATLAB:textio:io:BadVariableNames', namelengthmax));
|
| | 206 | end
|
| | 207 |
|
| | 208 | iscellofstrings = ~(iscellstr(newNames) || ischar(newNames) || isstring(newNames));
|
| | 209 | if iscellofstrings
|
| | 210 | error(message('MATLAB:makeUniqueStrings:InvalidInputStrings'));
|
| | 211 | end
|
| | 212 | nOld = obj.fast_var_opts.numVars;
|
| | 213 | nNew = numel(newNames);
|
| | 214 | if nOld ~= nNew
|
| | 215 | if nOld < nNew % Adding new names
|
| | 216 | % Make the new names unique, preserving the old names
|
| | 217 | obj.fast_var_opts = obj.fast_var_opts.addVars(nNew - nOld);
|
| | 218 | else
|
| | 219 | obj.fast_var_opts = obj.fast_var_opts.removeVars(nNew+1:nOld);
|
| | 220 | end
|
| | 221 |
|
| | 222 | obj = updatePerVarSizes(obj,nNew);
|
| | 223 | if obj.is_unbounded_selection && ~isequal(obj.selected_vars,':')
|
| | 224 | obj.selected_vars = [obj.selected_vars nOld+1:nNew];
|
| | 225 | end
|
| | 226 | % any selected names out of range should be removed
|
| | 227 | if ~ischar(obj.selected_vars)
|
| | 228 | obj.selected_vars(obj.selected_vars > nNew)=[];
|
| | 229 | end
|
| | 230 | end
|
| | 231 |
|
| | 232 | % If incoming names conflict with any pre-set existing names,
|
| | 233 | % make those new names unique. This should ensure that any
|
| | 234 | % names assigned by the user remain unique.
|
| | 235 | oldNames = obj.fast_var_opts.OptionsStruct.Names;
|
| | 236 | changedNames = ~strcmp(oldNames,newNames);
|
| | 237 | newNames(changedNames) = matlab.lang.makeUniqueStrings(newNames(changedNames),...
|
| | 238 | oldNames(~changedNames), namelengthmax); % Keep the old names that were not changed
|
| | 239 |
|
| | 240 | obj.fast_var_opts.Names = newNames;
|
| | 241 | obj.using_generated_names = false;
|
| | 242 | end
|
| | 243 |
|
| | 244 | function obj = set.NumVariables(obj,rhs)
|
| | 245 | % Expect a non-negative scalar integer
|
| | 246 | if ~isnumeric(rhs) || ~isscalar(rhs) || floor(rhs) ~= rhs || rhs < 0 || isinf(rhs)
|
| | 247 | error(message('MATLAB:textio:textio:ExpectedScalarInt'));
|
| | 248 | end
|
| | 249 |
|
| | 250 | obj.fast_var_opts = matlab.io.internal.FastVarOpts(rhs);
|
| | 251 | end
|
| | 252 | end
|
| | 253 |
|
| | 254 | methods (Abstract, Access = protected)
|
| | 255 | obj = updatePerVarSizes(obj,nNew);
|
| | 256 | addCustomPropertyGroups(opts,helper);
|
| | 257 | modifyCustomGroups(opts,helper);
|
| | 258 | verifyMaxVarSize(obj,n);
|
| | 259 | end
|
| | 260 |
|
| | 261 | methods % class functions
|
| | 262 | function opts = setvartype(opts,varargin)
|
| | 263 | %setvartype
|
| | 264 | % OPTS = setvartype(OPTS,TYPE) set all variables to the
|
| | 265 | % specified TYPE by name.
|
| | 266 | %
|
| | 267 | % OPTS = setvartype(OPTS,NAMES,TYPE) set the variables to the
|
| | 268 | % specified TYPE by name. NAMES can be a character vector or a
|
| | 269 | % cell array of character vectors.
|
| | 270 | %
|
| | 271 | % OPTS = setvartype(OPTS,INDEX,TYPE) set the variables to the
|
| | 272 | % specified TYPE by index. INDEX must be a vector of positive integers with
|
| | 273 | % values between 1 and the length of the VARIABLENAMES property of OPTS.
|
| | 274 | %
|
| | 275 | % TYPE can be any numeric type, 'string, 'char',
|
| | 276 | % 'datetime', 'duration, 'categorical' or 'logical'.
|
| | 277 | %
|
| | 278 | % See also
|
| | 279 | % setvaropts, getvaropts, detectImportOptions
|
| | 280 | % matlab.io.VariableImportOptions
|
| | 281 |
|
| | 282 | import matlab.io.internal.validators.validateCellStringInput;
|
| | 283 |
|
| | 284 | narginchk(2,3);
|
| | 285 | if nargout == 0
|
| | 286 | error(message('MATLAB:textio:io:NOLHS','setvartype','setvartype'))
|
| | 287 | end
|
| | 288 | v_opts = opts.fast_var_opts;
|
| | 289 | if nargin == 2
|
| | 290 | % setvartype(OPTS,TYPE) syntax
|
| | 291 | selection = 1:v_opts.numVars;
|
| | 292 | type = varargin{1};
|
| | 293 | elseif isnumeric(varargin{1})
|
| | 294 | selection = varargin{1};
|
| | 295 | type = varargin{2};
|
| | 296 | elseif islogical(varargin{1})
|
| | 297 | selection = find(varargin{1});
|
| | 298 | type = varargin{2};
|
| | 299 | else
|
| | 300 | selection = validateCellStringInput(convertStringsToChars(varargin{1}), 'SELECTION');
|
| | 301 | if iscell(selection) || ischar(selection)
|
| | 302 | % Get the appropriate numeric indices and error for unknown variable names.
|
| | 303 | selection = opts.getNumericSelection(selection);
|
| | 304 | end
|
| | 305 | type = convertStringsToChars(varargin{2});
|
| | 306 | end
|
| | 307 | % Convert to cellstr
|
| | 308 | try type = cellstr(type); catch
|
| | 309 | error(message('MATLAB:textio:textio:InvalidStringOrCellStringProperty','TYPES'));
|
| | 310 | end
|
| | 311 |
|
| | 312 | % Expand scalar
|
| | 313 | if isscalar(type)
|
| | 314 | type = repmat(type,size(selection));
|
| | 315 | elseif numel(type) ~= numel(selection)
|
| | 316 | error(message('MATLAB:textio:io:MismatchVarTypes'))
|
| | 317 | end
|
| | 318 |
|
| | 319 | % Set the underlying types
|
| | 320 | try
|
| | 321 | %opts.fast_var_opts = v_opts.overrideType(selection,type);
|
| | 322 | opts.fast_var_opts = v_opts.setTypes(selection, type);
|
| | 323 | catch ME
|
| | 324 | throw(ME)
|
| | 325 | end
|
| | 326 | end
|
| | 327 |
|
| | 328 | function opts = setvaropts(opts,varargin)
|
| | 329 | %setvaropts
|
| | 330 | % OPTS = setvaropts(OPTS,VARNAMES, ...) set the options of variables
|
| | 331 | % by name. VARNAMES can be a character vector or a cell array of
|
| | 332 | % character vectors containing variable names.
|
| | 333 | %
|
| | 334 | % OPTS = setvaropts(OPTS,INDEX,...) set the options of variables by index. INDEX
|
| | 335 | % must be a vector of positive integers with values between 1
|
| | 336 | % and the length of the VARIABLENAMES property of OPTS.
|
| | 337 | %
|
| | 338 | % OPTS = setvaropts(OPTS,...) set all variables to the options specified. NOTE: If
|
| | 339 | % the VARIABLETYPES property of OPTS list different types for different
|
| | 340 | % variables, only the options which are available for all types can be
|
| | 341 | % specified. If all the VARIABLETYPES of OPTS are the same, then the type
|
| | 342 | % specific options may be specified.
|
| | 343 | %
|
| | 344 | % OPTS = setvaropts(___,OPTION1,VALUE1,...,OPTIONK,VALUEK) set the selected options
|
| | 345 | % with the parameters OPTION1, ..., PARAMK, to VALUE1, ..., VALUEK respectively.
|
| | 346 | % NOTE: If the selection of variables have different types, only the options
|
| | 347 | % which are available for all types can be specified. If all the variable
|
| | 348 | % types of the variables are the same, then the type specific options may be
|
| | 349 | % specified.
|
| | 350 | %
|
| | 351 | % Type: Set variables to the specified TYPE. TYPE can be any numeric type, 'string, 'char',
|
| | 352 | % 'datetime', 'duration, 'categorical' or 'logical'.
|
| | 353 | %
|
| | 354 | % Valid parameter names available for all variable types are:
|
| | 355 | % FillValue - A scalar value to fill missing or
|
| | 356 | % unconvertible data
|
| | 357 | % TreatAsMissing - Text which is used in a file to represent
|
| | 358 | % missing data, e.g. 'NA'
|
| | 359 | % QuoteRule - How to treat quoted text.
|
| | 360 | % Prefixes - Prefix characters to be removed
|
| | 361 | % from variable on import
|
| | 362 | % Suffixes - Suffix characters to be removed
|
| | 363 | % from variable on import
|
| | 364 | %
|
| | 365 | % Numeric specific options:
|
| | 366 | % ExponentCharacter - Character which should be treated as exponents when
|
| | 367 | % converting text
|
| | 368 | % DecimalSeparator - Character used to separate the integer part of a
|
| | 369 | % number from the decimal part of the number
|
| | 370 | % ThousandsSeparator - Character used to separate the thousands place
|
| | 371 | % digits
|
| | 372 | % TrimNonNumeric - Logical used to specify that all
|
| | 373 | % prefixes and suffixes must be removed
|
| | 374 | % NumberSystem - Read a number using 'decimal', 'hex' or 'binary'
|
| | 375 | % as the NumberSystem. 'decimal' is the default setting.
|
| | 376 | %
|
| | 377 | % Text specific options:
|
| | 378 | % WhitespaceRule - How to treat whitespace surrounding text
|
| | 379 | %
|
| | 380 | % Datetime specific options:
|
| | 381 | % DatetimeFormat - Output format of the datetime array.
|
| | 382 | % InputFormat - The format to use when importing
|
| | 383 | % text as dates
|
| | 384 | % DatetimeLocale - The locale to be used when importing text as
|
| | 385 | % dates
|
| | 386 | %
|
| | 387 | % Duration specific options:
|
| | 388 | % DurationFormat - Output format of the duration array.
|
| | 389 | % InputFormat - The format to use when importing text as
|
| | 390 | % time
|
| | 391 | % FieldSeparator - The character used to separate
|
| | 392 | % fields in the duration text
|
| | 393 | % DecimalSeparator - Character used to separate the integer part of a
|
| | 394 | % number from the decimal part of
|
| | 395 | % the number; applies to the seconds
|
| | 396 | % field of the duration text
|
| | 397 | %
|
| | 398 | % Categorical specific options:
|
| | 399 | % Categories - List of expected categories
|
| | 400 | % Protected - Whether the output array is protected
|
| | 401 | % Ordinal - Whether the output array is ordinal
|
| | 402 | %
|
| | 403 | % Logical specific options:
|
| | 404 | % TrueSymbols - Text to be converted to the logical value
|
| | 405 | % true.
|
| | 406 | % FalseSymbols - Text to be converted to the logical value
|
| | 407 | % false.
|
| | 408 | % CaseSensitive - Whether or not to consider case when matching
|
| | 409 | % symbols
|
| | 410 | %
|
| | 411 | % Note: Certain variable import options control how text data (either in
|
| | 412 | % text files or spreadsheet files) are converted to their respective
|
| | 413 | % types. Native spreadsheet types which are converted to MATLAB types do
|
| | 414 | % not use these options. For example, dates in spreadsheet files are stored as a
|
| | 415 | % number of days since January 1st 1900, thus the InputFormat will not be
|
| | 416 | % considered. However, dates ealier than January 1st 1900 must be stored in
|
| | 417 | % spreadsheet files as text, and thus be converted using the value of InputFormat.
|
| | 418 | %
|
| | 419 | % See also
|
| | 420 | % setvartype, getvaropts, detectImportOptions, matlab.io.VariableImportOptions
|
| | 421 |
|
| | 422 | % Check the inputs for evenly matched N-V paris.
|
| | 423 |
|
| | 424 | narginchk(2,inf);
|
| | 425 | if nargout == 0
|
| | 426 | error(message('MATLAB:textio:io:NOLHS','setvaropts','setvaropts'))
|
| | 427 | end
|
| | 428 |
|
| | 429 | try
|
| | 430 | func = matlab.io.internal.functions.FunctionStore.getFunctionByName('setvaropts');
|
| | 431 | opts = func.validateAndExecute(opts,varargin{:});
|
| | 432 | catch ME
|
| | 433 | throw(ME);
|
| | 434 | end
|
| | 435 | end
|
| | 436 |
|
| | 437 | function vopts = getvaropts(opts,selection)
|
| | 438 | %getvaropts get variable options by name or number
|
| | 439 | % VOPTS = getvaropts(OPTS,NAMES) get the options for the variables
|
| | 440 | % with specified names. NAMES can be a character vector or cell
|
| | 441 | % array of character vectors.
|
| | 442 | %
|
| | 443 | % VOPTS = getvaropts(OPTS,INDEX) get the options for the variables
|
| | 444 | % in the specified index. INDEX must be a vector of positive integers with
|
| | 445 | % values between 1 and the length of the VARIABLENAMES property of OPTS.
|
| | 446 | %
|
| | 447 | % VOPTS = getvaropts(OPTS) get the options for ALL the variables
|
| | 448 | %
|
| | 449 | % See also
|
| | 450 | % setvaropts, setvartype, detectImportOptions, matlab.io.VariableImportOptions
|
| | 451 |
|
| | 452 | if ~exist('selection','var')
|
| | 453 | selection = ':';
|
| | 454 | end
|
| | 455 | selection = matlab.io.internal.validators.validateCellStringInput(selection, 'NAMES');
|
| | 456 | vopts = opts.fast_var_opts.getVarOpts(opts.fast_var_opts.fixSelection(selection));
|
| | 457 | end
|
| | 458 |
|
| | 459 | function tf = isequal(opts1,opts2)
|
| | 460 | % Import Options should be equal regardless of the missing
|
| | 461 | % comparisons in the FillValue properties.
|
| | 462 | tf = isequaln(opts1,opts2);
|
| | 463 | end
|
| | 464 |
|
| | 465 | function tf = isequaln(opts1,opts2)
|
| | 466 | tf = false;
|
| | 467 |
|
| | 468 | % Same classes?
|
| | 469 | if ~strcmp(class(opts1),class(opts2)); return; end
|
| | 470 |
|
| | 471 | % Same number of variables?
|
| | 472 | n1 = opts1.fast_var_opts.numVars();
|
| | 473 | n2 = opts2.fast_var_opts.numVars();
|
| | 474 | if n1 ~= n2; return; end
|
| | 475 |
|
| | 476 | % Same Variable Options?
|
| | 477 | vo1 = opts1.fast_var_opts.getVarOptsStruct(1:n1);
|
| | 478 | vo2 = opts2.fast_var_opts.getVarOptsStruct(1:n2);
|
| | 479 | if ~isequaln(vo1,vo2); return; end
|
| | 480 |
|
| | 481 | % remove the variable options to compare the remaining
|
| | 482 | % properties
|
| | 483 | opts1.fast_var_opts = opts1.fast_var_opts.removeVars(1:n1);
|
| | 484 | opts2.fast_var_opts = opts2.fast_var_opts.removeVars(1:n2);
|
| | 485 | % This field shouldn't be compared.
|
| | 486 | opts1.using_generated_names = opts2.using_generated_names;
|
| | 487 | tf = builtin('isequaln',opts1,opts2);
|
| | 488 | end
|
| | 489 | end
|
| | 490 |
|
| | 491 | methods (Access = private)
|
| | 492 | function idx = getNumericSelection(obj,selection)
|
| | 493 | selection = cellstr(selection);
|
| | 494 | if isscalar(selection) && strcmp(selection,':')
|
| | 495 | % Select Everything
|
| | 496 | idx = 1:obj.fast_var_opts.numVars;
|
| | 497 | else
|
| | 498 | [~,idx] = ismember(selection, obj.fast_var_opts.Names');
|
| | 499 | if any(idx==0)
|
| | 500 | error(message('MATLAB:textio:io:UnknownVarName',selection{find(idx==0,1)}));
|
| | 501 | end
|
| | 502 | end
|
| | 503 | end
|
| | 504 |
|
| | 505 | function selection = fixSelection(opts,selection)
|
| | 506 | if iscell(selection) || ischar(selection)
|
| | 507 | selection = opts.getNumericSelection(selection);
|
| | 508 | elseif isnumeric(selection)
|
| | 509 | if ~all(selection > 0 & isfinite(selection) & floor(selection)==selection & selection <= numel(opts.fast_var_opts))
|
| | 510 | error(message('MATLAB:textio:io:BadNumericSelection'));
|
| | 511 | end
|
| | 512 | else
|
| | 513 | error(message('MATLAB:textio:io:BadSelectionInput'));
|
| | 514 | end
|
| | 515 | end
|
| | 516 | end
|
| | 517 |
|
| | 518 | methods (Static, Hidden)
|
| | 519 | function [filename,opts,rrn,rvn,args] = validateReadtableInputs(filename,opts,args)
|
| | 520 | persistent parser
|
| | 521 | filename = matlab.io.internal.validators.validateFileName(filename);
|
| | 522 | % Choose the first match from the list of valid file names.
|
| | 523 | filename = filename{1};
|
| | 524 | if isempty(parser)
|
| | 525 | parser = inputParser();
|
| | 526 | parser.FunctionName = 'readtable';
|
| | 527 | parser.KeepUnmatched = true;
|
| | 528 | parser.addParameter('ReadVariableNames',false,@(tf)validateLogical(tf,'ReadVariableNames'));
|
| | 529 | parser.addParameter('ReadRowNames',false,@(tf)validateLogical(tf,'ReadRowNames'));
|
| | 530 | end
|
| | 531 | [args{:}] = convertStringsToChars(args{:});
|
| | 532 | parser.parse(args{:});
|
| | 533 | params = parser.Results;
|
| | 534 |
|
| | 535 | rrn = params.ReadRowNames;
|
| | 536 | if rrn && ~opts.usingRowNames()
|
| | 537 | % User didn't define a rownamesColumn, but called readtable with ReadRowNames
|
| | 538 | opts = opts.setRowNames(true);
|
| | 539 | elseif ~rrn && ~any(strcmp('ReadRowNames',parser.UsingDefaults)) && opts.usingRowNames()
|
| | 540 | % User specified a RowNamesColumn, but asked readtable not to import it.
|
| | 541 | % set the RowNames back to default
|
| | 542 | opts = opts.setRowNames(false);
|
| | 543 | end
|
| | 544 |
|
| | 545 | rvn = params.ReadVariableNames;
|
| | 546 |
|
| | 547 | end
|
| | 548 | end
|
| | 549 |
|
| | 550 | methods (Static, Access = protected)
|
| | 551 | function obj = setAllProps(obj,s)
|
| | 552 | if isfield(s,'var_opts') && isa(s.var_opts,'matlab.io.VariableImportOptions')
|
| | 553 | s.fast_var_opts = matlab.io.internal.FastVarOpts.fromFullVarOpts(s.var_opts);
|
| | 554 | s = rmfield(s, "var_opts");
|
| | 555 | end
|
| | 556 | obj.fast_var_opts = s.fast_var_opts;
|
| | 557 | s = rmfield(s, "fast_var_opts");
|
| | 558 | for f = fieldnames(s)'
|
| | 559 | obj.(f{1}) = s.(f{1}); % f is a 1x1 cell array containing the name of a field in struct s
|
| | 560 | end
|
| | 561 | end
|
| | 562 |
|
| | 563 | function obj = loadImportOptions(s, type)
|
| | 564 | % Helper method used when loading Import Options objects from a
|
| | 565 | % MAT file. This method sets the properties common to all
|
| | 566 | % Import Options object.
|
| | 567 | if isstruct(s)
|
| | 568 | obj = matlab.io.ImportOptions.getImportOptionsByType(type);
|
| | 569 |
|
| | 570 | % older import options saved VariableImportOptions
|
| | 571 | % instead of FastVarOpts
|
| | 572 | if ~isfield(s, "fast_var_opts")
|
| | 573 | s.fast_var_opts = matlab.io.internal.FastVarOpts.fromFullVarOpts(s.var_opts);
|
| | 574 | end
|
| | 575 |
|
| | 576 | if ~isfield(s, "is_unbounded_selection")
|
| | 577 | s.is_unbounded_selection = isequal(s.selected_vars,':');
|
| | 578 | end
|
| | 579 |
|
| | 580 | typeSpecificProps = obj.getTypeSpecificProperties();
|
| | 581 | commonProps = ["fast_var_opts", "ImportErrorRule", "MissingRule",...
|
| | 582 | "EmptyColumnType", "PreserveVariableNames",...
|
| | 583 | "selected_vars", "is_unbounded_selection"];
|
| | 584 | props = [commonProps typeSpecificProps];
|
| | 585 |
|
| | 586 | for ii = 1:numel(props)
|
| | 587 | try
|
| | 588 | % Some properties may not be fields in the struct
|
| | 589 | % if options were saved in earlier releases.
|
| | 590 | obj.(props(ii)) = s.(props(ii));
|
| | 591 | catch ME
|
| | 592 | % Expect a non Existent Field error if loading a
|
| | 593 | % new property that did not exist in an older
|
| | 594 | % release. In this case, the new property is set to
|
| | 595 | % the default value. Otherwise, we have an invalid
|
| | 596 | % value and we will issue the error as a
|
| | 597 | % warning.
|
| | 598 | if ~strcmp(ME.identifier, 'MATLAB:nonExistentField')
|
| | 599 | warning(ME.identifier, '%s', ME.message);
|
| | 600 | end
|
| | 601 | end
|
| | 602 | end
|
| | 603 |
|
| | 604 | if isfield(s, "using_generated_names")
|
| | 605 | obj.using_generated_names = s.using_generated_names;
|
| | 606 | else
|
| | 607 | % set using_generated_names to false if the struct does
|
| | 608 | % not have this field.
|
| | 609 | obj.using_generated_names = false;
|
| | 610 | end
|
| | 611 |
|
| | 612 | else % loading from an object
|
| | 613 | obj = s;
|
| | 614 |
|
| | 615 | % if numVars is 1, there is no way to kow if fast_var_opts
|
| | 616 | % was loaded from the MAT file or if it was constructed
|
| | 617 | % during the creation of a default ImportOptions object.
|
| | 618 | if obj.fast_var_opts.numVars() == 1
|
| | 619 | obj.fast_var_opts = matlab.io.internal.FastVarOpts.fromFullVarOpts(obj.var_opts);
|
| | 620 | end
|
| | 621 | obj.var_opts = [];
|
| | 622 | end
|
| | 623 | end
|
| | 624 | end
|
| | 625 |
|
| | 626 | methods(Static, Access = private)
|
| | 627 | function obj = getImportOptionsByType(type)
|
| | 628 | switch type
|
| | 629 | case "fixed"
|
| | 630 | obj = matlab.io.text.FixedWidthImportOptions;
|
| | 631 | case "delimited"
|
| | 632 | obj = matlab.io.text.DelimitedTextImportOptions;
|
| | 633 | case "spreadsheet"
|
| | 634 | obj = matlab.io.spreadsheet.SpreadsheetImportOptions;
|
| | 635 | otherwise
|
| | 636 | assert(false);
|
| | 637 | end
|
| | 638 | end
|
| | 639 | end
|
| | 640 |
|
| | 641 | methods(Static, Abstract, Access = protected)
|
| | 642 | props = getTypeSpecificProperties();
|
| | 643 | end
|
| | 644 |
|
| | 645 | methods (Hidden)
|
| | 646 |
|
| | 647 | function T = readtable(filename,opts,varargin)
|
| | 648 | if ~isa(opts,'matlab.io.ImportOptions')
|
| | 649 | error(message('MATLAB:textio:io:OptsSecondArg','readtable'))
|
| | 650 | end
|
| | 651 | try
|
| | 652 | func = matlab.io.internal.functions.FunctionStore.getFunctionByName('readtableWithImportOptions');
|
| | 653 | T = func.validateAndExecute(filename,opts,varargin{:});
|
| | 654 | catch ME
|
| | 655 | throw(ME);
|
| | 656 | end
|
| | 657 | end
|
| | 658 |
|
| | 659 | function TT = readtimetable(filename,opts,varargin)
|
| | 660 | if ~isa(opts,'matlab.io.ImportOptions')
|
| | 661 | error(message('MATLAB:textio:io:OptsSecondArg','readtimetable'))
|
| | 662 | end
|
| | 663 | try
|
| | 664 | func = matlab.io.internal.functions.FunctionStore.getFunctionByName('readtimetableWithImportOptions');
|
| | 665 | TT = func.validateAndExecute(filename,opts,varargin{:});
|
| | 666 | catch ME
|
| | 667 | throw(ME);
|
| | 668 | end
|
| | 669 | end
|
| | 670 |
|
| | 671 | function A = readmatrix(filename,opts,varargin)
|
| | 672 | if ~isa(opts,'matlab.io.ImportOptions')
|
| | 673 | error(message('MATLAB:textio:io:OptsSecondArg','readmatrix'))
|
| | 674 | end
|
| | 675 | try
|
| | 676 | func = matlab.io.internal.functions.FunctionStore.getFunctionByName('readmatrixWithImportOptions');
|
| | 677 | A = func.validateAndExecute(filename,opts,varargin{:});
|
| | 678 | catch ME
|
| | 679 | throw(ME);
|
| | 680 | end
|
| | 681 | end
|
| | 682 |
|
| | 683 | function C = readcell(filename,opts,varargin)
|
| | 684 | if ~isa(opts,'matlab.io.ImportOptions')
|
| | 685 | error(message('MATLAB:textio:io:OptsSecondArg','readcell'))
|
| | 686 | end
|
| | 687 | try
|
| | 688 | func = matlab.io.internal.functions.FunctionStore.getFunctionByName('readcellWithImportOptions');
|
| | 689 | C = func.validateAndExecute(filename,opts,varargin{:});
|
| | 690 | catch ME
|
| | 691 | throw(ME);
|
| | 692 | end
|
| | 693 | end
|
| | 694 |
|
| | 695 | function varargout = readvars(filename,opts,varargin)
|
| | 696 | if ~isa(opts,'matlab.io.ImportOptions')
|
| | 697 | error(message('MATLAB:textio:io:OptsSecondArg','readvars'))
|
| | 698 | end
|
| | 699 | try
|
| | 700 | func = matlab.io.internal.functions.FunctionStore.getFunctionByName('readvarsWithImportOptions');
|
| | 701 | [varargout{1:nargout}] = func.validateAndExecute(filename,opts,varargin{:});
|
| | 702 | catch ME
|
| | 703 | throw(ME);
|
| | 704 | end
|
| | 705 | end
|
| | 706 |
|
| | 707 | function disp(opts)
|
| | 708 | import matlab.io.internal.cellArrayDisp;
|
| | 709 | name = inputname(1);
|
| | 710 | h = matlab.internal.datatypes.DisplayHelper(class(opts));
|
| | 711 |
|
| | 712 | opts.addCustomPropertyGroups(h);
|
| | 713 |
|
| | 714 | replacePropDisp(h,"VariableNames",cellArrayDisp(opts.VariableNames,false,''));
|
| | 715 | replacePropDisp(h,"VariableTypes",cellArrayDisp(opts.VariableTypes,false,''));
|
| | 716 | replacePropDisp(h,"SelectedVariableNames",cellArrayDisp(opts.SelectedVariableNames,false,''));
|
| | 717 |
|
| | 718 | if h.usingHotlinks()
|
| | 719 | setVarHelp = h.helpTextLink("setvaropts", class(opts) + "/setvaropts");
|
| | 720 | getVarHelp = h.helpTextLink("getvaropts", class(opts) + "/getvaropts");
|
| | 721 | setvartypeText = h.helpTextLink("setvartype", class(opts) + "/setvartype");
|
| | 722 |
|
| | 723 | replacePropDisp(h,"VariableOptions",...
|
| | 724 | sprintf('Show all %d %s',numel(opts.VariableNames),h.propDisplayLink(name,"VariableOptions")));
|
| | 725 | else
|
| | 726 | setVarHelp = "setvaropts";
|
| | 727 | getVarHelp = "getvaropts";
|
| | 728 | setvartypeText = "setvartype";
|
| | 729 |
|
| | 730 | replacePropDisp(h,"VariableOptions",...
|
| | 731 | sprintf(['[1' getString(message('MATLAB:matrix:dimension_separator')) '%d %s]'],numel(opts.VariableNames),'matlab.io.VariableImportOptions'));
|
| | 732 | end
|
| | 733 |
|
| | 734 | appendPropDisp(h,"VariableOptions",...
|
| | 735 | sprintf("\n\tAccess VariableOptions sub-properties using %s/%s",setVarHelp,getVarHelp));
|
| | 736 |
|
| | 737 | % Display the PreserveVariableNames property.
|
| | 738 | replacePropDisp(h, "PreserveVariableNames", string(opts.PreserveVariableNames));
|
| | 739 |
|
| | 740 | if h.usingHotlinks()
|
| | 741 | previewHelp = h.helpTextLink("preview","matlab.io.text.TextImportOptions/preview");
|
| | 742 | else
|
| | 743 | previewHelp = "preview";
|
| | 744 | end
|
| | 745 |
|
| | 746 | if isa(opts,'matlab.io.spreadsheet.SpreadsheetImportOptions')
|
| | 747 | dispProp = "VariableDescriptionsRange";
|
| | 748 | else
|
| | 749 | dispProp = "VariableDescriptionsLine";
|
| | 750 | end
|
| | 751 | appendPropDisp(h,dispProp,sprintf('\n\t%s %s',...
|
| | 752 | getString(message('MATLAB:textio:io:TablePreview')),previewHelp));
|
| | 753 | opts.modifyCustomGroups(h);
|
| | 754 | appendPropDisp(h,"Variable Import Properties",sprintf("Set types by name using " + setvartypeText));
|
| | 755 | h.printToScreen("opts",false);
|
| | 756 | end
|
| | 757 |
|
| | 758 | function obj = saveobj(obj)
|
| | 759 | obj.var_opts = obj.VariableOptions;
|
| | 760 | end
|
| | 761 | end
|
| | 762 |
|
| | 763 | properties (Dependent, Access = {?matlab.io.internal.mixin.HasPropertiesAsNVPairs})
|
| | 764 | NumVariables
|
| | 765 | end
|
| | 766 |
|
| | 767 | methods (Hidden)
|
| | 768 | function opts = setUnboundedSelection(opts,isUnbounded)
|
| | 769 | opts.is_unbounded_selection = isUnbounded;
|
| | 770 | end
|
| | 771 | function tf = namesAreGenerated(opts)
|
| | 772 | tf = opts.using_generated_names;
|
| | 773 | end
|
| | 774 |
|
| | 775 | function opts = useGeneratedNames(opts,rnc)
|
| | 776 | opts.using_generated_names = true;
|
| | 777 | if rnc > 0 && rnc <= opts.fast_var_opts.numVars
|
| | 778 | opts.fast_var_opts = opts.fast_var_opts.setVarNames(rnc, {'Row'});
|
| | 779 | end
|
| | 780 | end
|
| | 781 |
|
| | 782 | function varopts = getVarOptsStruct(opts,idx)
|
| | 783 | varopts = opts.fast_var_opts.getVarOptsStruct(idx);
|
| | 784 | end
|
| | 785 |
|
| | 786 | end
|
| | 787 |
|
| | 788 | methods(Access = protected)
|
| | 789 | function s = saveToStruct(obj)
|
| | 790 | s = struct();
|
| | 791 | % properties defined in ImportOptions
|
| | 792 | s.selected_vars = obj.selected_vars;
|
| | 793 | s.is_unbounded_selection = obj.is_unbounded_selection;
|
| | 794 | s.using_generated_names = obj.using_generated_names;
|
| | 795 | s.fast_var_opts = obj.fast_var_opts;
|
| | 796 | s.EmptyColumnType = obj.EmptyColumnType;
|
| | 797 |
|
| | 798 | % properties defined in MissingErrorRulesInputs
|
| | 799 | s.ImportErrorRule = obj.ImportErrorRule;
|
| | 800 | s.MissingRule = obj.MissingRule;
|
| | 801 |
|
| | 802 | % properties defined in PreserveVariableNamesInput
|
| | 803 | s.PreserveVariableNames = obj.PreserveVariableNames;
|
| | 804 | end
|
| | 805 |
|
| | 806 | function obj = loadFromStruct(obj, s)
|
| | 807 | % properties defined in ImportOptions
|
| | 808 | obj = trySetProp(obj, s, "selected_vars");
|
| | 809 | obj = trySetProp(obj, s, "is_unbounded_selection");
|
| | 810 | obj = trySetProp(obj, s, "using_generated_names");
|
| | 811 | obj = trySetProp(obj, s, "fast_var_opts");
|
| | 812 | obj = trySetProp(obj, s, "EmptyColumnType");
|
| | 813 |
|
| | 814 | % properties defined in MissingErrorRulesInputs
|
| | 815 | obj = trySetProp(obj, s, "ImportErrorRule");
|
| | 816 | obj = trySetProp(obj, s, "MissingRule");
|
| | 817 |
|
| | 818 | % properties defined in PreserveVariableNamesInput
|
| | 819 | obj = trySetProp(obj, s, "PreserveVariableNames");
|
| | 820 |
|
| | 821 | end
|
| | 822 | end
|
| | 823 | end
|
| | 824 |
|
| | 825 | function obj = trySetProp(obj, s, prop)
|
| | 826 | % Tries to set property to the saved value.
|
| | 827 | try
|
| | 828 | obj.(prop) = s.(prop);
|
| | 829 | catch ME
|
| | 830 | % Don't warn if the property is not a field on the struct. This
|
| | 831 | % may happen when loading an object saved in a previous release.
|
| | 832 | if ~strcmp(ME.identifier, 'MATLAB:nonExistentField')
|
| | 833 | warning(message('MATLAB:io:xml:saveload:IncompatiblePropertyLoad',...
|
| | 834 | 'ImportOptions', prop));
|
| | 835 | end
|
| | 836 | end
|
| | 837 | end
|
| | 838 |
|
| | 839 | function validateLogical(tf,param)
|
| | 840 | if ~islogical(tf) && ~isnumeric(tf) || ~isscalar(tf)
|
| | 841 | error(message('MATLAB:table:InvalidLogicalVal',param));
|
| | 842 | end
|
| | 843 | end
|
| | 844 | % LocalWords: IMPORTERRORRULE omitrow omitvar MISSINGRULE
|
| | 845 | % LocalWords: SELECTEDVARIABLENAMES VARIABLENAMES VARIABLEOPTIONS setvaropts
|
| | 846 | % LocalWords: getvaropts VARIABLETYPES XFD importoptions setvartype NOLHS
|
| | 847 | % LocalWords: paris nv newnames VOPTS
|
| | 848 |
|