Importing a Table :: [Variables are been modified by Matlab]

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

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?)
Hi Steven
1. Some Names that have been modified are
DCDC_OutputOC_Fault_BOOL
DCDC_EEPROM_CRCfault_Fault_BOOL
and the name that was not modified are
DCDC_OTP_Fault_BOOL .
2. No Duplicates
3. I checked them with isvarname and I can also check them with iskeyword but I don't think that would be the reason.
I believe its something to do with my spreadsheet editing. I have another spreadsheet and the column names flawlessly become variables. Whereas in this spreadsheet I get something like
Columns 1 through 7
[1x33 char] [1x28 char] [1x23 char] [1x28 char] [1x32 char] [1x31 char] [1x29 char]
Columns 8 through 14
[1x24 char] [1x24 char] [1x22 char] [1x26 char] [1x28 char] [1x24 char] [1x24 char]
Columns 15 through 20
[1x24 char] 'DCDC_OTP_Fault_BOOL' [1x23 char] [1x23 char] 'DCDC_I2C_FAULT_BOOL' [1x31 char]
Columns 21 through 27
[1x24 char] [1x29 char] [1x31 char] [1x34 char] [1x29 char] [1x29 char] [1x30 char]
Columns 28 through 34
[1x33 char] [1x33 char] [1x35 char] [1x38 char] [1x28 char] [1x29 char] [1x43 char]
Column 35
[1x40 char]
Would it be practical to post the spreadsheet, or at least an initial portion of it?
Yes absolutely and here is the error/warning after my readtable('Filename')
readtable('C:\Users\rohit.ranjan\Desktop\VAT\02_Data_Experimaentation_IP_OP_Files\VAT_Experiment_Sheet_Assign_Table_Structure_Units.xlsx') Warning: Variable names were modified to make them valid MATLAB identifiers. > In genvalidnames at 81 In @table\private\setVarNames at 43 In table.readXLSFile at 113 In table.readFromFile at 43 In readtable at 114
Hi Guys,
Sorry I am fairly new to Matlab and the image I posted up only means that Matlab dosent have much space to show me the whole string and it is trying to show me that this is an array or characters by mentioning [1x28] . Thank you for all quick answers and prompts and help.

Sign in to comment.

 Accepted Answer

I don't see how it's being changed. Here is my code:
t = readtable('VAT_Experiment_Sheet_Assign_Table_Structure_Units.xlsx')
fieldnames(t)
and here is what I see:
ans =
'DCDC_CurrentLimiting_Warning_BOOL'
'DCDC_CoolantOTP_Warning_BOOL'
'DCDC_Wakeup_Status_BOOL'
'DCDC_UnitEnabled_Status_BOOL'
'DCDC_StartUpInitDone_Status_BOOL'
'DCDC_RemoteShutdown_Status_BOOL'
'DCDC_LatchedFault_Status_BOOL'
'DCDC_InputOk_Status_BOOL'
'DCDC_HVIL_OK_Status_BOOL'
'DCDC_Status_Fault_BOOL'
'DCDC_Thermistor_Fault_BOOL'
'DCDC_ShortCircuit_Fault_BOOL'
'DCDC_OutputUV_Fault_BOOL'
'DCDC_OutputOV_Fault_BOOL'
'DCDC_OutputOC_Fault_BOOL'
'DCDC_OTP_Fault_BOOL'
'DCDC_InputUV_Fault_BOOL'
'DCDC_InputOV_Fault_BOOL'
'DCDC_I2C_FAULT_BOOL'
'DCDC_EEPROM_CRCfault_Fault_BOOL'
'DCDC_CANfault_Fault_BOOL'
'DCDCReversPolarity_Fault_BOOL'
'DCDC_ReversPolarity_Status_BOOL'
'DCDC_Coolant_Temprature_VALUE_DEGC'
'DCDC_Output_CURRENT_VALUE_AMP'
'DCDC_Input_Voltage_VALUE_VOLT'
'DCDC_Output_Voltage_VALUE_VOLT'
'DCDC_Output_voltage_UV_Fault_BOOL'
'DCDC_Output_voltage_OV_Fault_BOOL'
'DCDC_Output_limit_Current_VALUE_AMP'
'DCDCOutput_setpoint_Voltage_VALUE_VOLT'
'DCDCDcDc_Set_index_byte_Mplx'
'DCDC_RemoteEnable_Status_ENUM'
'DCDC_Temperature_coolant_Warning_VALUE_DEGC'
'DCDC_Temperature_Coolant_Fault_BOOL_DEGC'
'Properties'
Unless I'm overlooking something, like a double underline or something, the "DCDC_OutputOC_Fault_BOOL" looks the same in both Excel and MATLAB.

4 Comments

Hi Image Analyst,
can you check the output of
ans.Properties.Variablenames ?
No. ans is just the fieldnames that get spit out. and is not an object or class so it won't have any properties or methods at all. You can do this with t though:
t = readtable('VAT_Experiment_Sheet_Assign_Table_Structure_Units.xlsx')
fieldnames(t) % Show column headers
methods(t) % Show functions t can do.
t.Properties.VariableNames
Again, I don't see any problems.
Variable names in the spreadsheet cannot have spaces.
As of release R2019b variable names in table arrays are allowed to contain spaces. They are no longer required to be valid variable names. [Note that in release R2020b which I am using the name of the option for readtable to preserve the variable names has changed, see the R2020b Release Notes by changing the release filter to see that note.] Cell E1 in scores.xlsx is "Winning Percentage" with a space.
>> t = readtable('c:\temp\scores.xlsx')
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before
creating variable names for the table. The original column headers are saved in the
VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
t =
4×5 table
Team Wins Losses Ties WinningPercentage
____________ ____ ______ ____ _________________
{'Bills' } 10 3 0 0.769230769230769
{'Dolphins'} 8 5 0 0.615384615384615
{'Patriots'} 6 7 0 0.461538461538462
{'Jets' } 0 12 0 0
>> t2 = readtable('c:\temp\scores.xlsx', 'VariableNamingRule', 'preserve')
t2 =
4×5 table
Team Wins Losses Ties Winning percentage
____________ ____ ______ ____ __________________
{'Bills' } 10 3 0 0.769230769230769
{'Dolphins'} 8 5 0 0.615384615384615
{'Patriots'} 6 7 0 0.461538461538462
{'Jets' } 0 12 0 0
>> t2{2, "Winning percentage"}
ans =
0.615384615384615

Sign in to comment.

More Answers (1)

The answer by Steven Lord didn't work for me. I got this error:
Error using readtable (line 198)
Unknown Parameter 'VariableNamingRule'.
But the following solution (as explained here) works for me in MATLAB R2020a:
readtable(filename,'PreserveVariableNames',true)

3 Comments

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.
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))
The option is VariableNamingRule and the value you have selected is preserve.
You can use parametersToUse as a comma-separated list.
parametersToUse{:}
ans = 'VariableNamingRule'
ans = 'preserve'

Sign in to comment.

Categories

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!