Can table variable names start with a number character?

1.9k Views Asked by At

I am running something like this:

table = readtable(path,'ReadVariableNames',true);

In the .csv file all of the cells of the first row contain string identifiers like '99BM', '105CL', etc. Everything starts with a number. The command above gives a table with variable names like 'x99BM', 'x105CL', etc.

Is it possible to get rid of this 'x'? I need to compare these identifiers with a column of another table that is clear of this 'x'.

2

There are 2 best solutions below

0
On BEST ANSWER

No, table variable names can't start with a number since they follow the same naming conventions as normal variables. The 'x' is automatically added by readtable to create a valid name. You should have noticed this warning when calling readtable:

Warning: Variable names were modified to make them valid MATLAB identifiers.
The original names are saved in the VariableDescriptions property.

So, you can't get rid of the 'x' within the table. But, if you need to do any comparisons, you can do them against the original values saved in the VariableDescriptions property, which will have this format:

>> T.Properties.VariableDescriptions

ans =

  1×2 cell array

    'Original column heading: '99BM''    'Original column heading: '105CL''

You can parse these with a regular expression, for example:

originalNames = regexp(T.Properties.VariableDescriptions, '''(.+)''', 'tokens', 'once');
originalNames = vertcat(originalNames{:});

originalNames =

  2×1 cell array

    '99BM'
    '105CL'

And then use these in any string comparisons you need to do.

0
On

No. As mentioned by gnovice, the readtable function automatically renames invalid names. It does this by calling matlab.lang.makevalidname and setting the output as the column name.

If I understand correctly, you're comparing the contents of a column from one table with the names of the columns of another table. In that case contains(['x' <contents of single row of column>], table.VariableNames) will prepend x to the value in a row of the table column (for this implementation, you need to loop through every row of the table) and then compare this string with the variable names of the table. You can also do this in a single line with arrayfun or something but I am doing this from memory right now and can't recall the correct syntax.