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!