Importing a Table :: [Variables are been modified by Matlab]
Show older comments
Hi I have recently started using matlab. I am trying to import a spreadsheet as a table and some of my variables [Colum Names] get modified when I look at them using T.Properties.Variablenames. So I checked the function genvalidnames which is apparently responsible for changing the "non-matlab" variable names to standard matlab variable names. My Column names in the Spreadsheet all start with a character, have only underscores and are not longer than nameslength which i checked is 63. Still I get the modified names. Any idea why this will be happening.
5 Comments
Steven Lord
on 28 Apr 2016
What specific names were modified?
Were any of the names in your spreadsheet duplicates?
Were any of the names that were modified MATLAB keywords (as listed by the iskeyword function?)
Tejas Sonavane
on 28 Apr 2016
Edited: Image Analyst
on 28 Apr 2016
Walter Roberson
on 28 Apr 2016
Would it be practical to post the spreadsheet, or at least an initial portion of it?
Tejas Sonavane
on 28 Apr 2016
Edited: Tejas Sonavane
on 28 Apr 2016
Tejas Sonavane
on 28 Apr 2016
Accepted Answer
More Answers (1)
Bill Tubbs
on 12 Oct 2021
Edited: Bill Tubbs
on 12 Oct 2021
The answer by Steven Lord didn't work for me. I got this error:
Error using readtable (line 198)
Unknown Parameter 'VariableNamingRule'.
readtable(filename,'PreserveVariableNames',true)
3 Comments
Steven Lord
on 12 Oct 2021
The PreserveVariableNames property (which could only take on values true and false) was replaced in release R2020b with the "VariableNamingRule" name-value pair (which had 'preserve' and 'modify' as valid values) as stated in the Release Notes. That's the change I mentioned in square brackets in my comment on Image Analyst's accepted answer.
Bill Tubbs
on 12 Oct 2021
Ah, sorry I missed that. Thanks. I guess I will have to update my code next year when I upgrade MATLAB...
You could future proof your code now while it's fresh in your mind. Use verLessThan to determine if the MATLAB installation is older than release R2020b. Use PreserveVariableNames if it is and VariableNamingRule if it is not.
if verLessThan('matlab', '9.9') % 20b
parametersToUse = {'PreserveVariableNames', true};
else
parametersToUse = {'VariableNamingRule', 'preserve'};
end
fprintf("The option is %s and the value you have selected is %s.\n", ...
string(parametersToUse))
You can use parametersToUse as a comma-separated list.
parametersToUse{:}
Categories
Find more on Data Type Identification in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!