time | Calls | line |
---|
| | 1 | function [variables,metadata,omittedvarsmap] = readSpreadsheet(filename,opts,args)
|
| | 2 | %READSPREADSHEET reads a spreadsheet file according to the import options
|
| | 3 |
|
| | 4 | % Copyright 2016-2019 The MathWorks, Inc.
|
0.003 | 2 | 5 | params = parseArguments(args);
|
| | 6 |
|
< 0.001 | 2 | 7 | if ~isempty(params.Sheet)
|
0.001 | 2 | 8 | opts.Sheet = params.Sheet;
|
| 2 | 9 | end
|
| | 10 |
|
< 0.001 | 2 | 11 | if ~ischar(filename) && isa(filename,'matlab.io.spreadsheet.internal.Worksheet')
|
< 0.001 | 2 | 12 | sheet = filename;
|
| | 13 | else
|
| | 14 | % first open the file.
|
| | 15 | ssFile = openFile(filename,params.UseExcel);
|
| | 16 | % then open the requested sheet
|
| | 17 | sheet = opts.Sheet;
|
| | 18 | if isempty(sheet)
|
| | 19 | sheet = 1;
|
| | 20 | end
|
| | 21 | % get the sheet obj
|
| | 22 | sheet = ssFile.getSheet(sheet);
|
< 0.001 | 2 | 23 | end
|
0.014 | 2 | 24 | [~,selectedIDs] = ismember(opts.SelectedVariableNames',opts.VariableNames);
|
| | 25 |
|
| | 26 | % DATARANGE
|
0.025 | 2 | 27 | [variables,errorIDs,missingIDs,dataRange] = handleDataRange(selectedIDs, opts, sheet, params.Preview, params.UseExcel);
|
| | 28 |
|
| | 29 | % VARIABLE NAMES
|
< 0.001 | 2 | 30 | numVars = numel(opts.VariableNames);
|
< 0.001 | 2 | 31 | if params.ReadVariableNames && ~isempty(opts.VariableNamesRange)
|
0.003 | 2 | 32 | metadata.VariableNames = readVariableMetadata(sheet,opts.VariableNamesRange,numVars);
|
< 0.001 | 2 | 33 | metadata.VariableNames = metadata.VariableNames(selectedIDs);
|
< 0.001 | 2 | 34 | empties = strlength(metadata.VariableNames) == 0;
|
< 0.001 | 2 | 35 | metadata.VariableNames(empties) = compose('Var%d',find(empties));
|
< 0.001 | 2 | 36 | if params.FixVariableNames
|
| | 37 | metadata.VariableNames = matlab.lang.makeValidName(metadata.VariableNames);
|
| | 38 | metadata.VariableNames = matlab.lang.makeUniqueStrings(metadata.VariableNames,{'RowNames','Properties'});
|
< 0.001 | 2 | 39 | end
|
| | 40 | else
|
| | 41 | metadata.VariableNames = opts.VariableNames(selectedIDs);
|
< 0.001 | 2 | 42 | end
|
| | 43 |
|
| | 44 | % VARIABLE UNITS
|
< 0.001 | 2 | 45 | metadata.VariableUnits = {};
|
< 0.001 | 2 | 46 | if ~isempty(opts.VariableUnitsRange)
|
| | 47 | metadata.VariableUnits = readVariableMetadata(sheet,opts.VariableUnitsRange,numVars);
|
| | 48 | metadata.VariableUnits = metadata.VariableUnits(selectedIDs);
|
< 0.001 | 2 | 49 | end
|
| | 50 |
|
| | 51 | % VARIABLE DESCRIPTIONS
|
< 0.001 | 2 | 52 | metadata.VariableDescriptions = {};
|
< 0.001 | 2 | 53 | if ~isempty(opts.VariableDescriptionsRange)
|
| | 54 | metadata.VariableDescriptions = readVariableMetadata(sheet,opts.VariableDescriptionsRange,numVars);
|
| | 55 | metadata.VariableDescriptions = metadata.VariableDescriptions(selectedIDs);
|
< 0.001 | 2 | 56 | end
|
| | 57 |
|
| | 58 | % ROWNAMES
|
< 0.001 | 2 | 59 | metadata.RowDimNames = {};
|
< 0.001 | 2 | 60 | metadata.RowNames = {};
|
< 0.001 | 2 | 61 | if ~isempty(opts.RowNamesRange)
|
0.003 | 1 | 62 | metadata.RowNames = readRowNames(sheet,opts.RowNamesRange,dataRange);
|
< 0.001 | 1 | 63 | emptyNames = cellfun('isempty',metadata.RowNames);
|
< 0.001 | 1 | 64 | emptyNames = emptyNames | any(params.TreatAsMissing(:)' == string(metadata.RowNames(:)),2);
|
| | 65 |
|
< 0.001 | 1 | 66 | if any(emptyNames(:))
|
| | 67 | metadata.RowNames(emptyNames) = compose('Row%d',find(emptyNames));
|
| 1 | 68 | end
|
0.002 | 1 | 69 | metadata.RowNames = matlab.lang.makeUniqueStrings(metadata.RowNames);
|
< 0.001 | 2 | 70 | end
|
0.003 | 2 | 71 | varOpts = opts.getVarOptsStruct(selectedIDs);
|
0.008 | 2 | 72 | [variables,metadata,omittedvarsmap] = handleReplacement(variables,metadata,varOpts,opts.ImportErrorRule,opts.MissingRule,errorIDs,missingIDs);
|
< 0.001 | 2 | 73 | end
|
Other subfunctions in this file are not included in this listing.