How can I create a table from variables (DATA, ColumnNames)

6 visualizzazioni (ultimi 30 giorni)
Hi,
I like to create a table containing the variable names and the corresponding data in individual columns.
The data (DATA.m / double) and the corresponding data names (DataNames.m / cell) are located in my matlab workspace, each in a separated variable of the dimension 1x100 (row).
How can I merge a table from these two variables which allocates the data names to the data? I found out that I have to provide the names as a string... e.g.
DataTable = table(DATA,'VariableNames',{'Name1' 'Name2' 'Name3' 'Name4' 'Name5' '...'});
Since there are quite a lot of names in the DataNames-variable I like to avoid this procedure and instead provide only the variable names to a command line... I tried the following command, but it doesn't work:
DataTable = table(Data,'VariableNames',DataNames);
Error: "The VariableNames property must contain one name for each variable in the table."
Is there a quick solution to my problem?
Best regards

Risposta accettata

Peter Perkins
Peter Perkins il 19 Feb 2016
Spostato: Stephen23 il 14 Feb 2024
Joe, you may be happy with where you ended up, but I'm not sure it's the best way.
You said:
"The data (DATA.m / double) and the corresponding data names (DataNames.m / cell) are located in my matlab workspace, each in a separated variable of the dimension 1x100 (row)."
I interpret that as, "I have an Nx100 double matrix, and a 1x100 cell array of strings, and I want to make an Nx100 table, containing one variable for each column of the matrix DATA." It may be that N is 1, but I'll get to that in a moment.
I think you want
DataTable = array2table(num2cell(DATA),'VariableNames',DataNames);
where DataNames is the cleaned-up version after replacing spaces and parentheses. (By the way, you could use genvarname or better yet, matlab.lang.makeValidName to do that cleaning up.)
If your N really is 1, then (clarity aside) it doesn't much matter if you do what you did or what I suggested. But for a large N, the cell array you get from num2cell is likely to take up a large amount of memory, much more than the original Nx100 double matrix.
Hope this helps.

Più risposte (0)

Categorie

Scopri di più su Tables in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by