How do I assign a different category list to each row in the same column using UITable in AppDesigner?

16 ビュー (過去 30 日間)
Hello,
One of the issues I am facing is that I'm trying to sort a list of categories depending on each row in a UITable. It seems that once I convert the column to categorical, Matlab does not permit me to change the categories of any of the rows individually. Can anyone point out my error or point me towards a workaround? Here is the problem.
% This line sets the categories for the whole column
app.UITable.Data.Var2 = categorical(app.UITable.Data.Var2, {'None',vals{:}});
% This line does not permit me to change the categories to a new order specified by index array j
app.UITable.Data.Var2(1) = categorical(app.UITable.Data.Var2(1), {'None',vals{j}});

回答 (1 件)

Akanksha
Akanksha 2025 年 6 月 10 日
You're trying to assign a new categorical value with a different set of categories to just one row using - app.UITable.Data.Var2(1) = categorical(app.UITable.Data.Var2(1), {'None', vals{j}});
This fails because MATLAB doesn't allow mixing different category sets within the same categorical array.
To update individual rows with new values, you must ensure the new value is part of the existing category set, you can opt for any the following workaround :
1. Define all possible categories once:
allCategories={'None',vals{:}};  % full list of possible categories
app.UITable.Data.Var2=categorical(app.UITable.Data.Var2,allCategories);
2. Assign new values using existing categories:
newValue=vals{j};  % must be one of the existing categories
app.UITable.Data.Var2(1)=categorical({newValue},allCategories);
The above workaround will ensure that you're not redefining the category set, just assigning a new value from the existing set.
Hope this helps!

カテゴリ

Help Center および File ExchangeDevelop Apps Using App Designer についてさらに検索

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by