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

조회 수: 6 (최근 30일)
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 CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by