Add names to table variables from a large cell

조회 수: 1 (최근 30일)
NA
NA 2021년 6월 2일
댓글: Stephen23 2021년 6월 9일
I have a 1x72 cell with name like 'Var_name'
Var_name = {[1,3,4],[4,7,8],[1,2],...,[4,6,7],[5,6,8]}; ---> 1x72 cell
and have a matrix A
A = [4 1 0 0...; 5 0 1 1...; 6 1 0 1...; 7 0 1 0...; ...]; ---> 10x72 double
I would like to have a Table that has variable names based on the above cell with Var_name, like this...
index [1,3,4] [4,7,8] [1,2] [4,6,7] [5,6,8]
4 1 0 0 ...
5 0 1 1 ...
6 1 0 1 ...
7 0 1 0 ...
. . . .
I used this code, but it does not work
Var_name_string = cellfun(@(a)string(a),Var_name,'uni',0);
T = array2table(A.','variablenames',{'index' Var_name_string});
instead of ---> T = array2table(A,'VariableNames',{'index' '{1,3,4}' '{4,7,8}' '{1,2}' ....'{4,6,7}' '[5,6,8]'});

채택된 답변

Stephen23
Stephen23 2021년 6월 2일
편집: Stephen23 2021년 6월 2일
% Fake data:
V = {[1,3,4],[4,7,8],[1,2]};
A = randi(9,5,4);
% Create table:
F = @(n)sprintf("{%s}",join(string(n),","));
T = array2table(A,'VariableNames',["Index",cellfun(F,V)])
T = 5×4 table
Index {1,3,4} {4,7,8} {1,2} _____ _______ _______ _____ 6 5 5 1 4 4 2 7 3 7 6 7 3 2 2 1 9 6 3 1
  댓글 수: 4
NA
NA 2021년 6월 9일
I have this Var_name
Var_name = {[1,3,4],[4,7,8],[1,2],...,[4,6,8,9,11,18,19,20,21,22,23,24,25,30,31,32,34,35,36,40,41,42,56]};
So, I got this error
Error using array2table (line 81)
'{4,6,8,9,11,18,19,20,21,22,23,24,25,30,31,32,34,35,36,40,41,42,56}' is
longer than the maximum allowed length for a table variable name.
Is there any way to fix it?
Stephen23
Stephen23 2021년 6월 9일
"Is there any way to fix it?"
Use a shorter name.

댓글을 달려면 로그인하십시오.

추가 답변 (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