How to make contents of a row as the name of the variables of a data table?

5 visualizaciones (últimos 30 días)
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);

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 20 de Abr. de 2023
Editada: Cris LaPierre el 20 de Abr. de 2023
To be variable names, your row must either contain character arrays or strings. See here.
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 comentarios
Maibam
Maibam el 20 de Abr. de 2023
Hi Cris. Thanks for the response.
The command data1.Properties.VariableNames = data1{41,:}; worked and it's the quickest alternative for my case here. However, for this to work I had to convert my dataset to table. Briefly, glancing over the difference between dataset and table, lokks like they are very similar and wondering why the same command wont apply to a dataset? I am a new Matlab user so this may be a silly question.
Cris LaPierre
Cris LaPierre el 20 de Abr. de 2023
Editada: Cris LaPierre el 20 de Abr. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Tables en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by