time | Calls | line |
---|
| | 384 | function vars = createVariables(types,sz)
|
| | 385 | % Create variables of the specified types, of the specified height,
|
| | 386 | % for a preallocated table, filled with each type's default value.
|
< 0.001 | 2 | 387 | nrows = sz(1);
|
< 0.001 | 2 | 388 | nvars = sz(2);
|
< 0.001 | 2 | 389 | vars = cell(1,nvars); % a row vector
|
< 0.001 | 2 | 390 | for ii = 1:nvars
|
< 0.001 | 10 | 391 | type = types{ii};
|
< 0.001 | 10 | 392 | switch type
|
< 0.001 | 10 | 393 | case {'double' 'single' 'logical'}
|
< 0.001 | 4 | 394 | vars{ii} = zeros(nrows,1,type);
|
< 0.001 | 6 | 395 | case {'doublenan' 'doubleNaN' 'singlenan' 'singleNaN'}
|
| | 396 | vars{ii} = NaN(nrows,1,extractBefore(lower(type),'nan'));
|
< 0.001 | 6 | 397 | case 'string'
|
0.003 | 2 | 398 | vars{ii} = repmat(string(missing),nrows,1);
|
< 0.001 | 4 | 399 | case 'cell'
|
| | 400 | vars{ii} = cell(nrows,1);
|
< 0.001 | 4 | 401 | case 'datetime'
|
| | 402 | vars{ii} = datetime.fromMillis(NaN(nrows,1));
|
< 0.001 | 4 | 403 | case 'duration'
|
| | 404 | vars{ii} = duration.fromMillis(zeros(nrows,1));
|
< 0.001 | 4 | 405 | case 'calendarDuration'
|
| | 406 | vars{ii} = calendarDuration(zeros(nrows,1),0,0);
|
< 0.001 | 4 | 407 | case 'categorical'
|
| | 408 | vars{ii} = categorical(NaN(nrows,1));
|
< 0.001 | 4 | 409 | case {'int8' 'int16' 'int32' 'int64' 'uint8' 'uint16' 'uint32' 'uint64'}
|
< 0.001 | 4 | 410 | vars{ii} = zeros(nrows,1,type);
|
| | 411 | case {'cellstr' 'char'}
|
| | 412 | if type == "char"
|
| | 413 | % Special case: replace 'char' with 'cellstr', with a warning. A char
|
| | 414 | % array is tempting but not a good choice for text data in a table.
|
| | 415 | matlab.internal.datatypes.warningWithoutTrace(message('MATLAB:table:PreallocateCharWarning'));
|
| | 416 | end
|
| | 417 | % cellstr is a special case that's not actually a type name.
|
| | 418 | vars{ii} = repmat({''},nrows,1);
|
| | 419 | otherwise
|
| | 420 | if nrows > 0 % lengthenVar requires n > 0
|
| | 421 | % Use lengthenVar to create a var of the correct height. Not
|
| | 422 | % all types have their name as a constructor, e.g. double.
|
| | 423 | % So instead of creating a scalar instance to lengthen,
|
| | 424 | % create an empty instance.
|
| | 425 | try
|
| | 426 | % Create 0x0, lengthenVar will turn it into an Nx1, but
|
| | 427 | % would turn a 1x0 into an Nx0 empty.
|
| | 428 | emptyVar = eval([type '.empty(0,0)']);
|
| | 429 | catch ME
|
| | 430 | throwAsCaller(preallocationClassErrorException(ME,type));
|
| | 431 | end
|
| | 432 | % lengthenVar creates an instance of var_ii that is nrows-by-1,
|
| | 433 | % filled in with the default (not necessarily "missing") value.
|
| | 434 | try
|
| | 435 | vars{ii} = tabular.lengthenVar(emptyVar,nrows);
|
| | 436 | catch
|
| | 437 | % lengthenVar failed, but we can still create an nrows-by-0 instance.
|
| | 438 | vars{ii} = eval([type '.empty(nrows,0)']); % don't use reshape, may not be one
|
| | 439 | end
|
| | 440 | else
|
| | 441 | try
|
| | 442 | vars{ii} = eval([type '.empty(0,1)']);
|
| | 443 | catch ME
|
| | 444 | throwAsCaller(preallocationClassErrorException(ME,type));
|
| | 445 | end
|
| | 446 | end
|
< 0.001 | 10 | 447 | end
|
< 0.001 | 10 | 448 | end
|
< 0.001 | 2 | 449 | end
|
Other subfunctions in this file are not included in this listing.