copy data from array to table

13 次查看(过去 30 天)
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