How to make contents of a row as the name of the variables of a data table?
11 views (last 30 days)
Show older comments
Hi! I have 70 columns in a data table that I imported from an excel file. The header/title for each column is in a row, but not as an actual variable name. So matlab gave it the default variable names (var1, var2...varN etc.). I want to replace the default names with the actual variable names that are in a specific row (say 40). is there a straighforward way to do this?
I tried the command below to change the variable names of the table but it threw an error. I tried parenthesis and curly brackets instead of the square brackets but it still won't work. Not sure what's wrong.
T.Properties.Variablenames = ['new_name1', 'new_name2',....]
Error using dataset/subsasgnDot
Unknown dataset property: VariableNames.
Error in indexing (line 84)
a = subsasgnDot(a,s,b);
0 Comments
Accepted Answer
Cris LaPierre
on 20 Apr 2023
Edited: Cris LaPierre
on 20 Apr 2023
You assign the names using the syntax below.
% cell array of character vectors
T.Properties.VariableNames = {'name1','name2',...}
% or string array
T.Properties.VariableNames = ["name1","name2",...]
% if row 40 of the table T contains character or strings
T.Properties.VariableNames = T{40,:}
2 Comments
Cris LaPierre
on 20 Apr 2023
Edited: Cris LaPierre
on 20 Apr 2023
I didn't realize you were working with a dataset and not a table. I haven't looked into the differences, but they are different data types so I would not expect them to have the same properties.
I do want to point out that tables are the preferred approach. From the dataset doc page:
The dataset data type is not recommended. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.
More Answers (0)
See Also
Categories
Find more on Tables in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!