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