How do I change a string object into a variable name that heatmap accepts

6 ビュー (過去 30 日間)
Ted H
Ted H 2023 年 1 月 4 日
コメント済み: Ted H 2023 年 1 月 4 日
I have a string array of table variable names that i want to feed into heatmap. I use 'for' to loop through a table to generate heatmaps that go to power point.
heatmap(engine_config, group_perms(i,1), group_perms(i,2))
I get this error.
Error using heatmap
'XVariable' value does not refer to a valid variable in the source table.
Caused by:
Unrecognized table variable name 'group1'.
heatmap wants this, not string:
heatmap(engine_config, 'group_1', 'group_2')
This is probably a real simple solution. I have not nailed down the use of brackets/parenthases to get the right data type.
I have tried some matlab conversions but no luck.
Any help is appreciated.
group_perms 10x2 string
group1 group2
group1 group4
group1 group5
group1 group3
group2 group4
group2 group5
group2 group3
group4 group5
group4 group3
group5 group3
obtained using
group_perms = nchoosek(group_vars, 2);
group_vars is a string array of a subset of variable names in my large dataset. I use my string array throughout my program.

採用された回答

Adam Danz
Adam Danz 2023 年 1 月 4 日
編集済み: Adam Danz 2023 年 1 月 4 日
If nchoosek returns a cell array of strings, you can index them using { }
try this.
heatmap(engine_config, group_perms{i,1}, group_perms{i,2})
or this to convert it to string array
group_perms = string(nchoosek(group_vars, 2));
heatmap(engine_config, group_perms(i,1), group_perms(i,2))
  3 件のコメント
Adam Danz
Adam Danz 2023 年 1 月 4 日
For my own sanity, could you show me exactly what this returns:
group_perms(i,1)
As well as this
engine_config.Properties.VariableNames
assuming this is your table.
Ted H
Ted H 2023 年 1 月 4 日
Thanks. This found my problem. There was a variable name in the string array that ended up not being in my table (its a large table with changes). And I read the error message as being a data type issue, not a missing object issue.
heatmap(engine_config, group_perms(i,1), group_perms(i,2))
is the correct syntax.
Thanks!

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

その他の回答 (1 件)

Steven Lord
Steven Lord 2023 年 1 月 4 日
This seems to work with a simpler example.
T = array2table(magic(5))
T = 5×5 table
Var1 Var2 Var3 Var4 Var5 ____ ____ ____ ____ ____ 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9
V = string(T.Properties.VariableNames)
V = 1×5 string array
"Var1" "Var2" "Var3" "Var4" "Var5"
heatmap(T, V(2), V(5)) % Using variables 2 and 5 as an example
Looking at your code, the example you posted that you said worked used "group_1" and "group_2" as variable names but the error message calls out that "group1" (no underscore) is not a variable in your table. What do your group_perms and group_vars variables contain: "group_1" or "group1"?
  1 件のコメント
Ted H
Ted H 2023 年 1 月 4 日
See comment in other answer. I did find the problem and it is a missing object (wrong variable name), not a data type problem, which I assumed when i read the error.

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

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

製品


リリース

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by