フィルターのクリア

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]
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 件のコメント
Stephen23
Stephen23 2023 年 7 月 13 日
編集済み: Stephen23 2023 年 7 月 13 日
"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.

サインインしてコメントする。

採用された回答

Angelo Yeo
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)
mytable = 5×3 table
var1 var2 var3 ____ ____ ____ 1 10 100 2 20 200 3 30 300 4 40 400 5 50 500
  1 件のコメント
영훈 정
영훈 정 2023 年 7 月 13 日
Wow, are you a genius? I think I've been looking at the wall for two hours. Thank you very much.

サインインしてコメントする。

その他の回答 (1 件)

Stephen23
Stephen23 2023 年 7 月 13 日
編集済み: Stephen23 2023 年 7 月 13 日
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

カテゴリ

Help Center および File ExchangeTables についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by