copy data from array to table

조회 수: 12 (최근 30일)
Marc Elmeua
Marc Elmeua 2020년 2월 9일
댓글: Marc Elmeua 2020년 2월 9일
Dear all,
I have an array with n number of columns and 100 rows. I would like to copy them into a table with the same dimension that has the names of the variables already written.
any ideas?
Thanks in advance!

채택된 답변

BN
BN 2020년 2월 9일
편집: BN 2020년 2월 9일
Hello, If A is your array and you want to have a table based on it, you can simply usearray2table.
Table = array2table(A);
If you want to change the variable names, so you can do this nicely, otherwise variable names stay what they are at the A
Table = array2table(A,'VariableNames',{'firstvariable','secondvariable','etc...'})
Here is an example for you:
A = rand(10,5); % here is random array with 10 rows and 5 columns
Table = array2table(A,'VariableNames',{'varname_1','varname_2','varname_3','varname_4','varname_5'}); %convert it to table and change the variable names
Best Regards
  댓글 수: 5
BN
BN 2020년 2월 9일
편집: BN 2020년 2월 9일
Hello Marc, This turns a different question rather than the first one, I think.
Anyways I explain it to you using this example, I think you can use the second code at the end of this comment as your answer from me.
Here we have two arrays, we transform them to the table. one of our tables has variable names and one other has not any variable names. We simply copy variable names of the first table into a cell and then use this cell to put variable names on our other table.
% I made this little and simple example to tell you how to:
% 1: copy variable names of one table
% 2: paste it as the variable names of second table automaticly
%Create 2 random arrays
A = rand(2,2);
B = rand(2,2);
% one of our arrays has variable name
A = array2table(T1,'VariableNames',{'first_variable','second_variable'});
% and one another has not any variable names.
% We want to copy variable name from A and paste it for B
% So first we get variable names from A:
varNames = A.Properties.VariableNames; % You can see one cell appears in
% workspace that contains name of variables in table A.
% now we want put this variable names as variable names of B (second table)
% So:
B = array2table(B,'VariableNames',varNames);
Mark, Note that if you already have 2 tables rather than arrays it would be very simple and all you should have to do is this two line:
% if A is your first table with variable names
varNames = A.Properties.VariableNames;
% and B is your second table without variable names
% you can use this to add A variable names to B
B.Properties.VariableNames = varNames;
Best Regards
Marc Elmeua
Marc Elmeua 2020년 2월 9일
Thanks Behzad, that's exactly what I needed.
I do have an array and a table, but once I transform the array to table I have two tables. So that's perfect! thanks!

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

추가 답변 (0개)

카테고리

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

제품

Community Treasure Hunt

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

Start Hunting!

Translated by