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.
0 个评论
采纳的回答
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 个评论
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 Center 和 File Exchange 中查找有关 Software Development Tools 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!