time | Calls | line |
---|
| | 1 | function [types,metaRows,emptyTrailing] = detectTypes(typeIDs,emptyColumnType,metaRows,readVarNames)
|
| | 2 | %
|
| | 3 |
|
| | 4 | % Copyright 2016-2019 The MathWorks, Inc.
|
< 0.001 | 2 | 5 | persistent enum
|
< 0.001 | 2 | 6 | persistent typenames
|
| | 7 |
|
< 0.001 | 2 | 8 | if isempty(typenames)
|
| | 9 | enum.NUMBER = matlab.io.spreadsheet.internal.Sheet.NUMBER;
|
| | 10 | enum.STRING = matlab.io.spreadsheet.internal.Sheet.STRING;
|
| | 11 | enum.DATETIME = matlab.io.spreadsheet.internal.Sheet.DATETIME;
|
| | 12 | enum.BOOLEAN = matlab.io.spreadsheet.internal.Sheet.BOOLEAN;
|
| | 13 | enum.EMPTY = matlab.io.spreadsheet.internal.Sheet.EMPTY;
|
| | 14 | enum.BLANK = matlab.io.spreadsheet.internal.Sheet.BLANK;
|
| | 15 | enum.ERROR = matlab.io.spreadsheet.internal.Sheet.ERROR;
|
| | 16 | % enum.DURATION = matlab.io.spreadsheet.internal.Sheet.DURATION;
|
| | 17 | % these names are in the same position as the enum number value, the ''
|
| | 18 | % values correspond to empty, blank and error.
|
| | 19 | typenames = {'double','char','datetime','logical','','','','duration','hexadecimal','binary'};
|
< 0.001 | 2 | 20 | end
|
| | 21 |
|
< 0.001 | 2 | 22 | if ~exist('readVarNames','var')
|
| | 23 | readVarNames = true;
|
< 0.001 | 2 | 24 | end
|
| | 25 |
|
| | 26 | % Find the trailing columns which do not contain data.
|
< 0.001 | 2 | 27 | emptyTrailing = find(~all(typeIDs==enum.EMPTY|typeIDs==enum.BLANK,1),1,'last')+1;
|
| | 28 |
|
< 0.001 | 2 | 29 | if ~isempty(emptyColumnType) && strcmp(emptyColumnType,'double')
|
< 0.001 | 2 | 30 | EMPTY_TYPE = enum.NUMBER;
|
| | 31 | else
|
| | 32 | EMPTY_TYPE = enum.STRING;
|
< 0.001 | 2 | 33 | end
|
| | 34 |
|
< 0.001 | 2 | 35 | if nargin <= 2 || isempty(metaRows)
|
< 0.001 | 2 | 36 | textmask = (typeIDs == enum.STRING)|...
|
| 2 | 37 | (typeIDs == enum.BLANK)|...
|
| 2 | 38 | (typeIDs == enum.ERROR)|...
|
| 2 | 39 | (typeIDs == enum.EMPTY);
|
| | 40 |
|
| | 41 | % Find the first row that doesn't contain all strings/blanks/errors
|
< 0.001 | 2 | 42 | metaRows = min([size(typeIDs,1),find(~all(textmask,2),1)-1]);
|
| | 43 |
|
| | 44 | % If all data are string, do not detect metadata rows aside from
|
| | 45 | % variable names
|
< 0.001 | 2 | 46 | if metaRows == size(typeIDs,1)
|
| | 47 | % always include varname line unless readVarNames is false
|
| | 48 | metaRows = min(size(typeIDs,1),double(readVarNames));
|
| | 49 |
|
| | 50 | types = typenames(getDominantType(typeIDs,EMPTY_TYPE,metaRows,enum));
|
| | 51 | return
|
< 0.001 | 2 | 52 | end
|
< 0.001 | 2 | 53 | end
|
| | 54 |
|
| | 55 | % Convert to double to add NaNs to take advantage of MODE ignoring NaN
|
< 0.001 | 2 | 56 | typeIDs = double(typeIDs);
|
| | 57 | % Ignore all these
|
< 0.001 | 2 | 58 | typeIDs(typeIDs == enum.BLANK | typeIDs == enum.ERROR | typeIDs == enum.EMPTY) = NaN;
|
| | 59 | % Compute the dominant type of all rows excluding metaRows
|
0.004 | 2 | 60 | dominantType = mode(typeIDs((metaRows+1):end,:),1);
|
| | 61 |
|
< 0.001 | 2 | 62 | if any(dominantType ~= enum.STRING) && (metaRows > 0)
|
| | 63 | % Check each row
|
< 0.001 | 2 | 64 | isStrings = (dominantType == enum.STRING);
|
| | 65 | % not everything is string, otherwise detection returns early.
|
< 0.001 | 2 | 66 | for i = 1:metaRows
|
< 0.001 | 2 | 67 | rowTypes = typeIDs(i,:);
|
0.003 | 2 | 68 | isBlankOrText = isStrings|ismissing(rowTypes);
|
< 0.001 | 2 | 69 | if ~all(isBlankOrText) ... % blank rows don't matter
|
| 2 | 70 | && all(isBlankOrText | (rowTypes == dominantType) )
|
| | 71 | metaRows = i-1;
|
| | 72 | break;
|
< 0.001 | 2 | 73 | end
|
< 0.001 | 2 | 74 | end
|
| | 75 | elseif all(dominantType == enum.STRING) && (metaRows > 0)
|
| | 76 | % If all columns had String dominantType, the only metadata that
|
| | 77 | % can be detected are variable names
|
| | 78 | metaRows = min(size(typeIDs,1),double(readVarNames));
|
< 0.001 | 2 | 79 | end
|
| | 80 |
|
0.002 | 2 | 81 | types = typenames(getDominantType(typeIDs,EMPTY_TYPE,metaRows,enum));
|
< 0.001 | 2 | 82 | end
|
Other subfunctions in this file are not included in this listing.