Concatenate arrays in MATLAB and Change Column name of Table

조회 수: 6 (최근 30일)
Furqan Hashim
Furqan Hashim 2021년 1월 15일
댓글: Steven Lord 2021년 1월 15일
I am trying to concatenate a cell array and a double but can't achieve that. Cell array can be generated using below code.
test = {'A.X' 'B.Y' 'C.Z'} ;
Double array can be generated using below code.
test_double = [10 20 30; 40 50 60];
I tried concatenating them using following code
cat(1, test, test_double)
But I get dimension error as follows
Dimensions of matrices being concatenated are not consistent.
Also if I convert the double array to table and rename the columns using input form cell array it gives me an error
'A.X' is not a valid variable name.
Above error can be replicated using following code
test_table = table(test_double);
test_table.Properties.VariableNames = test;
How can I concat the arrays?
How can I convert double array to table and rename columns using cell array?

답변 (1개)

KSSV
KSSV 2021년 1월 15일
t = [10 20 30; 40 50 60];
A = t(:,1) ; B = t(:,2) ; C = t(:,3) ;
T = table(A,B,C)
  댓글 수: 4
Furqan Hashim
Furqan Hashim 2021년 1월 15일
If variables can't be named as suggested can below 2 arrays be concatenated?
test = {'A.X' 'B.Y' 'C.Z'} ;
test_double = [10 20 30; 40 50 60];
Steven Lord
Steven Lord 2021년 1월 15일
test = {'A.X' 'B.Y' 'C.Z'} ;
test_double = [10 20 30; 40 50 60];
T = array2table(test_double, 'VariableNames', test)
T = 2x3 table
A.X B.Y C.Z ___ ___ ___ 10 20 30 40 50 60
It does require slightly different syntax to access the variables by name using dot notation if the names aren't MATLAB identifiers.
% T.A.X would not work
x = T.('A.X')
x = 2×1
10 40
Indexing using parentheses or curly braces works, though.
z2 = T{2, 'C.Z'}
z2 = 60

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

카테고리

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

제품


릴리스

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by