I want to know the process of changing the array to table. Too many table variables because there are too many arrays.
15 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
I need to combine several arrays and put them in one table. And I have to put the name var1 var2 var3 on each of them.
There is a good built-in function called array2 table. However, this built-in function must be entered directly as {'var1', 'var2', 'var3'} when specifying a variable.
a=[1; 2; 3; 4; 5;] ;
b=[10; 20; 30; 40; 50;];
c=[100; 200; 300; 400; 500;];
arraydata = [a b c]
tabledata= array2table(arraydata, 'VariableNames', {'var1', 'var2', 'var3'})
But I have too many arrays to enter all of this. Too many to enter 'varXX' one by one.
I thought it would be possible if I checked the size of the array and made a txt data with 'varxx'.
arraysize=size(arraydata,2)
strcollect=[];
for i= 1:1:arraysize
subtxt = 'var';
savename= strcat(subtxt,int2str(i))
strcollect=[strcollect savename]
end
I don't know how to collect str data. This result is different from what I thought.
tabledata= array2table(arraydata, 'VariableNames', strcollect)
The variablename is not the right size and type, so an error occurs. Is there any other way?
1 commentaire
Réponse acceptée
Angelo Yeo
le 13 Juil 2023
Please see if it works for you.
a=[1; 2; 3; 4; 5;] ;
b=[10; 20; 30; 40; 50;];
c=[100; 200; 300; 400; 500;];
arraydata = [a b c];
tempStr = struct;
for i = 1:3 % # vars
tempStr.("var"+i) = arraydata(:,i);
end
mytable = struct2table(tempStr)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!