Dynamic variable names for MATLAB table

11 ビュー (過去 30 日間)
Jay Vaidya
Jay Vaidya 2020 年 11 月 16 日
編集済み: Stephen23 2020 年 11 月 16 日
I am making the header names of the table as following:
colnames = [arrayfun(@(x) sprintf('type%d', x), 1:size(all_sorted), 'UniformOutput', false) arrayfun(@(x) sprintf('binary%d', x), 1:size(all_sorted), 'UniformOutput', false) 'result']
a_table = table(result_matrix, 'VariableNames',cell2mat(strcat({str2mat(colnames)})))
But always the last command gives error about
Error using table (line 259)
Invalid parameter name. Parameter name must be a nonempty string or character vector.
I tried a few permutations and combinations of converting the colnames to string, cell2mat, etc. But they give some or the other error. How can I specify the colnames with the above commands or with any of your other suggestions?
  1 件のコメント
Ameer Hamza
Ameer Hamza 2020 年 11 月 16 日
The statement 1:size(all_sorted) can be confusing and may not mean what you think it means. Explicitly specifying the dimension will be helpful.

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

採用された回答

Stephen23
Stephen23 2020 年 11 月 16 日
編集済み: Stephen23 2020 年 11 月 16 日
  • Use array2table instead of table (the wrong function, unless you really want the array in one variable).
  • Get rid of all of that cell2mat and strcat and str2mat confusion (what are you concatenating? Nothing).
all_sorted = [2;3;5];
result_matrix = randi(5,7)
result_matrix = 7×7
5 1 1 1 1 2 2 2 2 4 1 4 5 1 5 5 2 4 5 3 3 2 1 5 4 4 2 4 3 3 2 5 1 4 5 3 5 4 3 1 3 3 4 3 4 5 5 2 2
nr = size(all_sorted,1);
C1 = arrayfun(@(x) sprintf('type%d', x), 1:nr, 'UniformOutput', false)
C1 = 1x3 cell array
{'type1'} {'type2'} {'type3'}
C2 = arrayfun(@(x) sprintf('binary%d', x), 1:nr, 'UniformOutput', false)
C2 = 1x3 cell array
{'binary1'} {'binary2'} {'binary3'}
VN = [C1,C2,{'result'}];
T = array2table(result_matrix, 'VariableNames',VN)
T = 7x7 table
type1 type2 type3 binary1 binary2 binary3 result _____ _____ _____ _______ _______ _______ ______ 5 1 1 1 1 2 2 2 2 4 1 4 5 1 5 5 2 4 5 3 3 2 1 5 4 4 2 4 3 3 2 5 1 4 5 3 5 4 3 1 3 3 4 3 4 5 5 2 2

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Type Conversion についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by