copy data from array to table

13 views (last 30 days)
Marc Elmeua
Marc Elmeua on 9 Feb 2020
Commented: Marc Elmeua on 9 Feb 2020
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!

Accepted Answer

BN
BN on 9 Feb 2020
Edited: BN on 9 Feb 2020
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 Comments
BN
BN on 9 Feb 2020
Edited: BN on 9 Feb 2020
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 on 9 Feb 2020
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!

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!