MATLAB Answers

Change categorical array categories in table for use in uitable dropdown

81 views (last 30 days)
vik
vik on 8 Feb 2019
Answered: Tommy M on 6 Mar 2019
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 Comments

Sign in to comment.

Answers (2)

Luna
Luna on 8 Feb 2019
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.

  0 Comments

Sign in to comment.


Tommy M
Tommy M on 6 Mar 2019
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 Comments

Sign in to comment.

Sign in to answer this question.

Products


Release

R2018b

Translated by