I want to know the process of changing the array to table. Too many table variables because there are too many arrays.
8 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
영훈 정
el 13 de Jul. de 2023
Comentada: Angelo Yeo
el 13 de Jul. de 2023
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 comentario
Respuesta aceptada
Angelo Yeo
el 13 de Jul. de 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)
Ver también
Categorías
Más información sobre Tables en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!