I want to know the process of changing the array to table. Too many table variables because there are too many arrays.
조회 수: 8 (최근 30일)
이전 댓글 표시
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
채택된 답변
Angelo Yeo
2023년 7월 13일
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)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!