Clear Filters
Clear Filters

I want to know the process of changing the array to table. Too many table variables because there are too many arrays.

8 views (last 30 days)
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]
arraydata = 5×3
1 10 100 2 20 200 3 30 300 4 40 400 5 50 500
tabledata= array2table(arraydata, 'VariableNames', {'var1', 'var2', 'var3'})
tabledata = 5×3 table
var1 var2 var3 ____ ____ ____ 1 10 100 2 20 200 3 30 300 4 40 400 5 50 500
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)
arraysize = 3
strcollect=[];
for i= 1:1:arraysize
subtxt = 'var';
savename= strcat(subtxt,int2str(i))
strcollect=[strcollect savename]
end
savename = 'var1'
strcollect = 'var1'
savename = 'var2'
strcollect = 'var1var2'
savename = 'var3'
strcollect = 'var1var2var3'
I don't know how to collect str data. This result is different from what I thought.
tabledata= array2table(arraydata, 'VariableNames', strcollect)
Error using array2table
The VariableNames property is a cell array of character vectors. To assign multiple variable names, specify nonempty names in a string array or a cell array of character vectors.
The variablename is not the right size and type, so an error occurs. Is there any other way?
  1 Comment
Stephen23
Stephen23 on 13 Jul 2023
Edited: Stephen23 on 13 Jul 2023
"Is there any other way?"
Yes!, Do NOT use a loop for this, as if MATLAB was just some ugly low-level language.
Ignore any answer with a loop or that uses intermediate structure arrays or the like.

Sign in to comment.

Accepted Answer

Angelo Yeo
Angelo Yeo on 13 Jul 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)
mytable = 5×3 table
var1 var2 var3 ____ ____ ____ 1 10 100 2 20 200 3 30 300 4 40 400 5 50 500

More Answers (1)

Stephen23
Stephen23 on 13 Jul 2023
Edited: Stephen23 on 13 Jul 2023
The simple solution is to use CELLSTR, which gives ARRAY2TABLE the cell of char vectors that it requires:
M = (1:5).'*[1,10,100]
M = 5×3
1 10 100 2 20 200 3 30 300 4 40 400 5 50 500
T = array2table(M, 'VariableNames',cellstr("var"+(1:3)))
T = 5×3 table
var1 var2 var3 ____ ____ ____ 1 10 100 2 20 200 3 30 300 4 40 400 5 50 500

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!