read csv file: variable name consists of multiple words: resulting table variable name

I readtable a csv file of the format
date new york
---------------------
1/1/2021 15
1/2/2021 20
Then the resulting table has
class_Var1 class_NewYork
----------------------------
datetime double
But I just want to have a variable name 'New York'. Is it possible?

 Respuesta aceptada

If you have R2020b+, then you can use a modifier:
tableName = readtable('filename.csv','VariableNamingRule','preserve')
If you have R2019b+, then you can use the modifier:
tablename = readtable('filename.csv','preservevariablenames',true)
Credit to Walter Roberson for noticing this typo in the documentatation.

6 comentarios

In R2019b and R2020a, you instead need to use 'preservevariablenames', true
'variablenamingrule', 'preserve' is the revised syntax as of R2020b
Maybe you should suggest a revision to the documentation then...
It says 2019b in the documentation, though I have not yet tested the functionality.
Starting in R2019b, variable names and row names can include any characters, including spaces and non-ASCII characters. Also, they can start with any characters, not just letters. Variable and row names do not have to be valid MATLAB identifiers (as determined by the isvarname function). To preserve these variable names and row names, set the value of VariableNamingRule to 'preserve'.
Update: I just confirmed. The functionality does not work in R2020a, so I will edit the answer.
It is not really a typo in the documentation. It is correct that starting in R2019b, variable names can include any characters. The part about "To preserve" does not exactly continue on "Starting in R2019b": that part is about what to do in current releases, without mentioning the history.
I agree that it is easy enough to check, however, it does not mention the correct date in the section with the flag. I think including R2019b in this section is misleading, since it implies that this flag is compatible with R2019b.
Again, you are assuming that the "Starting in R2019b" applies to the whole paragraph, but it does not.
Consider "Starting in 1979, I no longer lived in the house I grew up in. I live in central Canada." Does that mean that I have lived in central Canada all of my life? Does that mean that I have lived in central Canada since 1979? Does the second sentance somehow become incorrect when you know that I have lived in central Canada since the early 1990s, and if so where is the error, since both parts are individually true?

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Preguntada:

el 24 de En. de 2021

Comentada:

el 24 de En. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by