I am using the below command to read a csv file into a table called T.
T = readtable("library.csv")
Now, the first row of the csv inludes the column headers, or variable names you can say. However, I want to change those variable names as I want. I have inspected readtable() documentation, but could not find the solution.
How can I do this?
Thanks.

 Réponse acceptée

Ameer Hamza
Ameer Hamza le 4 Avr 2020
Modifié(e) : Ameer Hamza le 4 Avr 2020

3 votes

One way is to change the variable names after reading the file like this.
t = readtable("library.csv");
t.Properties.VariableNames = {'name1', 'name2'}; % names of columns

6 commentaires

Stephan Piotrowski
Stephan Piotrowski le 3 Nov 2021
Certain variable names can cause a nuissance warning that clutters the command window. To avoid this, readtable can be called with "VariableNamingRule","preserve" as detailed in the documentation. But can the variable names in the source data be ignored entirely so that this does not have to be done? Perhaps by calling with ReadVariableNames set to false and DataRange set to 2?
Supriya Gain
Supriya Gain le 18 Mai 2022
In my case readtable automatically generate column header as "VAR". If I do not want to generate that "VAR" what should I do?
Stephen23
Stephen23 le 18 Mai 2022
"If I do not want to generate that "VAR" what should I do?"
Table columns/variables must have headers. If the header is not imported from a file, the default VAR* is used.
You can specify the header names when importing the file data, or replace them afterwards (see the above answer). What do you expect to see as the header names in your table?
Supriya Gain
Supriya Gain le 18 Mai 2022
Actually I'm converting.xls file to .CSV by using the command 'readtable'. Now in my actual dataset there was no header. But after converting using 'readtable' a default header is coming which is 'Var'. Now I don't want this to come. What should I do?
Walter Roberson
Walter Roberson le 18 Mai 2022
table() objects must have unique names for each variable. It is not possible to create a table() object which does not have variable names.
You can readtable() and pass VariableNames at the time of reading, thereby giving some name of your choice.
You can skip a header by using HeaderLines, 1 and ReadVariableNames, false
Stephen23
Stephen23 le 18 Mai 2022
@Supriya Gain: use READCELL and WRITECELL instead.

Connectez-vous pour commenter.

Plus de réponses (1)

Steven Lord
Steven Lord le 18 Mai 2022

0 votes

If you're using release R2020a or later I would use the renamevars function.

Community Treasure Hunt

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

Start Hunting!

Translated by