copy data from array to table
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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!
Réponse acceptée
BN
le 9 Fév 2020
Modifié(e) : BN
le 9 Fév 2020
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 commentaires
BN
le 9 Fév 2020
Modifié(e) : BN
le 9 Fév 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
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Logical dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!