uitable ColumnName from string array which keeps changing

조회 수: 1 (최근 30일)
Milos Krsmanovic
Milos Krsmanovic 2021년 9월 1일
댓글: Walter Roberson 2021년 9월 2일
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