필터 지우기
필터 지우기

Introduce columns in a table- Variables

조회 수: 1 (최근 30일)
Patrick Brown
Patrick Brown 2017년 2월 7일
댓글: Patrick Brown 2017년 2월 8일
Hi, I want to introduce columns in a table, then I write Table.a but a is a variable string and I want that the column has the name of this string how I can do that???

채택된 답변

Steven Lord
Steven Lord 2017년 2월 8일
This is possible using a slight variation of the cyclist's initial approach.
% Sample data
A = magic(4);
% Create the table to which you want to add data
T = array2table(A, 'VariableNames', {'first', 'second', 'third', 'fourth'})
% Define the new variable and the data it should contain
newvariable = 'fifth';
x = [20; 17; 8; 2];
% Add the new variable to the table T
T.(newvariable) = x

추가 답변 (1개)

the cyclist
the cyclist 2017년 2월 7일
% Create the table
x = rand(3,1);
tbl = table(x);
% Create the string that contains the variable name
varString = 'x';
% Access that variable, using the string
tbl(:,varString)
  댓글 수: 2
Patrick Brown
Patrick Brown 2017년 2월 8일
thanks for the answer
it is really tricky I think because the thing I want maybe it is no possible
for example a='velocity' - a string and when I create a column in a table Table.a I want that the name will be Table.velocity
maybe it is no possible but thanks again
the cyclist
the cyclist 2017년 2월 8일
This is also possible:
% Create the string that contains the variable name
varString = {'velocity'};
% Create the table with the name
x = rand(3,1);
tbl = table(x,'VariableName',varString);

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

카테고리

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