uitable ColumnName from string array which keeps changing

1 次查看(过去 30 天)
I have a string array (a global variable) that's changing throughout the script. One iteration would be:
critMaterial = [ "Steel" "Stainless" "Titanium" "Aluminum" "Bronze" "Brass" "Copper" "Lead" ];
I also have a function that is plotting a bunch of uitables together with some subplots based on various criteria. uitable column names should match critMaterials strings, analogue to:
uitable(fig,'Data',data,...
'ColumnName',{"Steel" "Stainless" "Titanium" "Aluminum" "Bronze" "Brass" "Copper" "Lead" },...
'RowName',{'X [unit]';'Y [unit]'});
Now this being a function I need to automate how ColumnName is populated. Quick and crass way I came up with is:
uitable(fig,'Data',data,...
'ColumnName',{ critMaterial(1), critMaterial(2), etc., critMaterial(7)},...
'RowName',{'X [unit]';'Y [unit]'});
Apart from being cumbersome I also need to monitor what is the maximum number of cells in critMaterial and match that number.
Is there a more elegant way of doing this? How can I loop through critMateral from 1 to numel(critMaterial) and assign the column names in each iteration automatically?
While searching for the solution, I found this answer. However, that topic discusses pre-set VariableNames which I do not have.
Thanks.

采纳的回答

Walter Roberson
Walter Roberson 2021-9-2
uitable(fig,'Data',data,...
'ColumnName', critMaterial(1:7),...
'RowName',{'X [unit]';'Y [unit]'});
You are permitted to pass a string array for that parameter, even though that is not documented as a possibility.
If you still have problems, then
uitable(fig,'Data',data,...
'ColumnName', cellstr(critMaterial(1:7)),...
'RowName',{'X [unit]';'Y [unit]'});
  2 个评论
Milos Krsmanovic
Milos Krsmanovic 2021-9-2
Thank you for your answer @Walter Roberson.
I actually tried the first recommendation before and received the error:
Cell array can contain only non-empty character vectors, string vectors, or numbers.
I received the same error for the second recommendation (cellstr) but after some research on the error this is what ultimately worked for me:
'ColumnName',[critMaterial(1:e)],...
where e=numel(critMaterial).
Not sure what the issue was exactly but it works now. Thanks again!
Walter Roberson
Walter Roberson 2021-9-2
uitable(fig,'Data',data,...
'ColumnName',{"Steel" "Stainless" "Titanium" "Aluminum" "Bronze" "Brass" "Copper" "Lead" },...
'RowName',{'X [unit]';'Y [unit]'});
That version sets eight column names.
critMaterial = [ "Steel" "Stainless" "Titanium" "Aluminum" "Bronze" "Brass" "Copper" "Lead" ];
That has eight entries, and numel() of that would be 8, so critMaterial(1:e) would be CritMaterial(1:8) would would have eight entries.
uitable(fig,'Data',data,...
'ColumnName',{ critMaterial(1), critMaterial(2), etc., critMaterial(7)},...
'RowName',{'X [unit]';'Y [unit]'});
but there you were asking for seven column names, and my suggestions such as
uitable(fig,'Data',data,...
'ColumnName', critMaterial(1:7),...
'RowName',{'X [unit]';'Y [unit]'});
corresponded to that seven . If you had eight columns but tried to set only seven column names, then you would expect there to be a problem.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Software Development Tools 的更多信息

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by