Change categorical array categories in table for use in uitable dropdown
조회 수: 17 (최근 30일)
이전 댓글 표시
I'm programming an App Designer GUI with uitable. Some table columns are categorical to do drop-down. On occasion, I want to extend or shorten the categories in table dropdown without changing anything else in the table.
However, if use setcats or assign a new categorical array to the table cell, the drop-down-menu will not change because the new categories are not assigned to the table data.
I wrote a small example code attached to demonstrate this, its basically like in the manual https://de.mathworks.com/help/matlab/creating_guis/display-an-interactive-table-in-app-designer.html : there is a Table with Row "Color" which has two Categories Blue and Red and I try to assign a new categorical array with Green:
tdata.Color(1) = categorical({'Red'},{'Blue','Red','Green'})
The categories will not be updated unless there is a new Value. categories(tdata.Color(1)) will now not include Green.
The attached example shows:
- The top button "Color(1) = Red" does the line of code above. Dropdown doesnt get updated, tdata does not include new categories.
- The middle button "Color(1) = Green" uses value 'Green' with same cats as above, this changes all drop-down-menus in that row.
- The bottom button "Add Row" adds a Row with value "Blue" and three categories. Now "Green" gets added to the dropdown.
How can I change the categories of a table column and the dropdown-list without changing anything else in this table?
댓글 수: 0
답변 (2개)
Luna
2019년 2월 8일
Hi Vik,
I don't know why it didn't work, I have tried what you did above. I tried exact the same thing with categorical variables and table. There should be reason why it does not work, otherwise it looks like a bug.
I have done this kind of thing before with cell arrays as follows. You can try it, it works.
f = uifigure;
cnames = 'Blue'; % Column Color Data
cVars = {{'Blue','Red'},[]}; % Column variables for Colors column and empty for second column. (If you don't want to make the other columns dropdown you should put [] for each column)
w = [400]; % Second column data
tdata = {cnames,w}; % Your table data (You should define it as cell array, if you define as a table 'ColumnFormat' property becomes useless)
uit = uitable(f,'Data',tdata,'ColumnEditable',true,'ColumnFormat',cVars,'ColumnName',{'Color','Wavelength'}); % table creation with ColumnName (this will be your table data's variables right now)
%% Now change cVars as 3 categories as new cVars
cVars_new = {{'Blue','Red','Green'},[]};
uit.ColumnFormat = cVars_new; % Change uitable's ColumnFormat property with the new variables.
댓글 수: 1
Arnab Samaddar-Chaudhuri
2023년 2월 13일
Tried this out and it works. If the data contains multiple rows, a little modification is needed but it will work based on the same principle.
Tommy M
2019년 3월 6일
This is definitely a bug in R2018b. I’ve tried renamecats, setcats and direct =. The changes only seem to take if there is a change in one of the item’s values (and maybe other's but this works) – and of course most times you don’t want to change anything other than the categories. So, I wrote an incredibly bad generic routine that will take in a string of the app table categorical array you want to change (inlude 'app.' in the text). Had to use eval because the categorical variable can't be passed in as a reference. Until the Matlab bug is fixed here goes:
function num_undefined = guiUpdateCategories(app,categorical_in,new_selections)
if sum(isundefined(eval(categorical_in))) ~= 0
uialert(app.UIFigure,'<guiUpdateCategories> Undefined Inputs','Info');
end
% 60% of the time it works every time
%categories(eval(categorical_in))
sels = cellstr(eval(categorical_in));
sels_save = sels;
sels{1} = 'asdfasd';
junk = categorical(sels,{'asdfasd'});
command = [categorical_in, ' = junk;'];
eval(command);
%categories(eval(categorical_in))
junk2 = categorical(sels_save,new_selections);
command = [categorical_in, ' = junk2;'];
eval(command);
%categories(eval(categorical_in))
num_undefined = sum(isundefined(eval(categorical_in)));
if num_undefined ~= 0
uialert(app.UIFigure,'<guiUpdateCategories> Undefined Outputs','Warning');
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!