time | Calls | line |
---|
| | 722 | function [validNames, modified] = makeValidName(names, modException)
|
| | 723 | %MAKEVALIDNAME Construct valid MATLAB identifiers from input names
|
| | 724 | % MAKEVALIDNAME is a private function for table that wraps
|
| | 725 | % around MATLAB.LANG.MAKEVALIDNAME. It adds exception control
|
| | 726 | % for when input names contains invalid identifier.
|
| | 727 | %
|
| | 728 | % MODEXCEPTION controls warning or error response when NAMES
|
| | 729 | % contains invalid MATLAB identifiers. Valid values for
|
| | 730 | % MODEXCEPTION are 'error', 'warnSavedLong', 'warn', 'warnSaved',
|
| | 731 | % 'silent', and 'error'. 'error' and 'warnSavedLong' are for names that
|
| | 732 | % do not need to be valid MATLAB identifiers, but still must follow some
|
| | 733 | % rules. 'warn', 'warnSaved', and 'silent' are for patching up names to
|
| | 734 | % valid MATLAB identifiers and exist for backward compatibility.
|
| | 735 | import matlab.internal.datatypes.warningWithoutTrace;
|
| | 736 | import matlab.internal.tabular.private.varNamesDim.checkReservedNames;
|
| | 737 | import matlab.internal.tabular.validateVariableNameLength;
|
| | 738 |
|
< 0.001 | 8 | 739 | if strcmp(modException,'error')
|
| | 740 | % For variable names that no longer need to be valid MATLAB identifiers,
|
| | 741 | % check length and conflicts with reserved names.
|
< 0.001 | 8 | 742 | validNames = names; % return the originals, or possibly error
|
< 0.001 | 8 | 743 | if ischar(names), names = { names }; end % unusual case, not optimized
|
| | 744 | % error if empty, too long, or reserved.
|
0.004 | 8 | 745 | validateVariableNameLength(names,'MATLAB:table:VariableNameLengthMax');
|
0.003 | 8 | 746 | checkReservedNames(names);
|
< 0.001 | 8 | 747 | modified = false(size(names));
|
| | 748 | elseif strcmp(modException, 'warnLength')
|
| | 749 | % For variable names that no longer need to be valid MATLAB identifiers,
|
| | 750 | % check length and conflicts with reserved names. If a tabular method is
|
| | 751 | % making a name, warn rather than erroring if the name is too long or
|
| | 752 | % conflicts with a reserved name.
|
| | 753 | validNames = names; % return the originals, or possibly error
|
| | 754 | if ischar(names), names = { names }; end % unusual case, not optimized
|
| | 755 | tooLong = validateVariableNameLength(names,'MATLAB:table:VariableNameLengthMax');
|
| | 756 | conflicts = checkReservedNames(names);
|
| | 757 | modified = tooLong | conflicts;
|
| | 758 | if any(modified)
|
| | 759 | % Unlike in the 'warn' case below that calls makeValidName, here
|
| | 760 | % makeUniqueStrings fixes both names that are too long and names that
|
| | 761 | % conflict with reservedNames.
|
| | 762 | validNames(modified) = matlab.lang.makeUniqueStrings(validNames(modified),validNames,namelengthmax);
|
| | 763 | if any(conflicts)
|
| | 764 | reservedNameConflicts = names(conflicts);
|
| | 765 | warningWithoutTrace(message('MATLAB:table:ModifiedVarnamesReservedConflict',reservedNameConflicts{1}));
|
| | 766 | end
|
| | 767 | if any(tooLong)
|
| | 768 | warningWithoutTrace(message('MATLAB:table:ModifiedVarnamesLengthMax'));
|
| | 769 | end
|
| | 770 | end
|
| | 771 | elseif strcmp(modException, 'warnSaved') || strcmp(modException, 'resolveConflict')
|
| | 772 | % For variable names that no longer need to be valid MATLAB identifiers,
|
| | 773 | % check length and conflicts with reserved names. Warn if any need to be
|
| | 774 | % patched up because of length or conflicts.
|
| | 775 | if strcmp(modException, 'resolveConflict')
|
| | 776 | % handle the case when the names contain empty char ''
|
| | 777 | emptyNames = matches(names,'');
|
| | 778 | names(emptyNames) = {'x'};
|
| | 779 | end
|
| | 780 | validNames = names; % return the originals, or possibly error
|
| | 781 | if ischar(names), names = { names }; end % unusual case, not optimized
|
| | 782 | tooLong = validateVariableNameLength(names,'MATLAB:table:VariableNameLengthMax');
|
| | 783 | conflicts = checkReservedNames(names);
|
| | 784 | modified = tooLong | conflicts;
|
| | 785 | if any(modified)
|
| | 786 | % Unlike in the 'warn' case below that calls makeValidName, here
|
| | 787 | % makeUniqueStrings fixes both names that are too long and names that
|
| | 788 | % conflict with reservedNames.
|
| | 789 | validNames(modified) = matlab.lang.makeUniqueStrings(validNames(modified),validNames,namelengthmax);
|
| | 790 | if strcmp(modException, 'warnSaved')
|
| | 791 | if any(conflicts)
|
| | 792 | reservedNameConflicts = names(conflicts);
|
| | 793 | warningWithoutTrace(message('MATLAB:table:ModifiedAndSavedVarnamesReserved',reservedNameConflicts{1}));
|
| | 794 | end
|
| | 795 | if any(tooLong)
|
| | 796 | warningWithoutTrace(message('MATLAB:table:ModifiedAndSavedVarnamesLengthMax'));
|
| | 797 | end
|
| | 798 | end
|
| | 799 | end
|
| | 800 | else % 'warn', 'warnSavedLegacy', 'silent'
|
| | 801 | [validNames, modified] = matlab.lang.makeValidName(names);
|
| | 802 | conflicted = checkReservedNames(validNames);
|
| | 803 | if any(conflicted)
|
| | 804 | validNames(conflicted) = matlab.lang.makeUniqueStrings(validNames(conflicted),validNames,namelengthmax);
|
| | 805 | end
|
| | 806 | modified = modified | conflicted;
|
| | 807 | if any(modified)
|
| | 808 | switch modException % error or warn per level specified
|
| | 809 | case 'warn'
|
| | 810 | warningWithoutTrace(message('MATLAB:table:ModifiedVarnames'));
|
| | 811 | case 'warnSavedLegacy'
|
| | 812 | warningWithoutTrace(message('MATLAB:table:ModifiedAndSavedVarnames'));
|
| | 813 | case 'warnUnstack'
|
| | 814 | warningWithoutTrace(message('MATLAB:table:ModifiedVarnamesUnstack'));
|
| | 815 | case 'warnRows2Vars'
|
| | 816 | warningWithoutTrace(message('MATLAB:table:ModifiedVarnamesRows2Vars'));
|
| | 817 | case 'silent'
|
| | 818 | % Used by summary and other functions where we would like to convert to
|
| | 819 | % valid MATLAB identifiers without displaying any warning
|
| | 820 | otherwise
|
| | 821 | assert(false);
|
| | 822 | end
|
| | 823 | end
|
< 0.001 | 8 | 824 | end
|
< 0.001 | 8 | 825 | end
|
Other subfunctions in this file are not included in this listing.