www.gusucode.com > datastoreio工具箱 matlab源码程序 > datastoreio/+matlab/+io/+datastore/@SpreadsheetDatastore/convertReaderData.m
function data = convertReaderData(ds, numRowsToRead) %convertReaderData function responsible for conversion % Copyright 2015-2016 The MathWorks, Inc. % imports import matlab.io.spreadsheet.internal.readSpreadsheetFile; % setup the options structure. rdOpts = getBasicReadOpts(ds, ds.BookObject, ds.SheetObject, false, ds.TextType); if nargin < 2 numRowsToRead = 'sheetOrFile'; end data = table; sVarNames = ds.SelectedVariableNames; % Make the dimension names unique with respect to the var names, silently data.Properties.DimensionNames = matlab.lang.makeUniqueStrings(data.Properties.DimensionNames,sVarNames,namelengthmax); % populating the table, taking ordering into account for sVarIdx = 1:numel(sVarNames) dataRange = getVarRange(ds, sVarIdx, numRowsToRead); rdOpts.range = dataRange; out = readSpreadsheetFile(rdOpts); try convertedData = convertToType(out.variables, ds.SelectedVariableTypes{sVarIdx}, ds.TextType); data.(sVarNames{sVarIdx}) = convertedData; catch e error(message('MATLAB:datastoreio:spreadsheetdatastore:conversionFail', ds.SelectedVariableNames{sVarIdx}, ds.SheetObject.Name, ds.CurrInfo.Filename, ds.SelectedVariableTypes{sVarIdx})); end end if isnumeric(numRowsToRead) if numRowsToRead <= ds.NumRowsAvailableInSheet ds.CurrRangeVector = ds.CurrRangeVector + [numRowsToRead 0 -numRowsToRead 0]; ds.NumRowsAvailableInSheet = ds.NumRowsAvailableInSheet - numRowsToRead; else ds.NumRowsAvailableInSheet = 0; end end end function dataRange = getVarRange(ds, varIdx, numRowsToRead) %GETVARRANGE This function is responsible for returning the range for the %selected variable based on its index and the number of rows to read sVarNamesIdx = ds.SelectedVariableNamesIdx; readSize = ds.ReadSize; dataIdx = find(sVarNamesIdx(varIdx) == 1:numel(ds.VariableNames)); % update the dataRange dataRange = ds.CurrRangeVector; dataRange(4) = 1; dataRange(2) = dataRange(2) + dataIdx - 1; if isnumeric(readSize) && numRowsToRead <= ds.NumRowsAvailableInSheet dataRange(3) = numRowsToRead; end end function [data, tf] = convertToType(convertedData, type, textType) %CONVERTTOTYPE This function is responsible for converting data lines to %the apprpriate types. tf = true; data = convertedData{1}; switch type case 'double' % throw is the data is not double assert(isa(data, 'double')); case 'datetime' if isa(data, 'double') && all(isnan(data)) % If all the values are NaN convert them to NaT data = NaT(size(data), 'Format', 'default'); else % force convert data to datetime and error if not possible % double data would error here. data = datetime(data); end case 'categorical' data = categorical(data); case 'char' if isa(data, 'double') data = strtrim(cellstr(num2str(data))); elseif isdatetime(data) data = strrep(cellstr(data), 'NaT', ''); elseif isequal(textType, 'string') % convert string array to cellstr data = cellstr(data); end case 'string' if isa(data, 'cell') && isequal(textType, 'char') % convert cellstr to string array data = string(data); end end end