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

조회 수: 6 (최근 30일)
Joe
Joe 2016년 2월 17일
이동: Stephen23 2024년 2월 14일
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

채택된 답변

Peter Perkins
Peter Perkins 2016년 2월 19일
이동: Stephen23 2024년 2월 14일
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.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Tables에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by