Drop down in uitable to work like excel

4 ビュー (過去 30 日間)
Antonio Anguita Vega
Antonio Anguita Vega 2024 年 10 月 3 日
I have an uitable and I want to have certain cells with a drop down. I know that this is done by using the functions categorical and table. The problem is that I need the table to accept empty values and show the empty values as a blank cell with the drop down functionality. If I add the empty option to a categorical this will show as <undefined>
Data = cell(21,2);
for i = 1:length(Data)
for j = 1:length(Data)
Data{i,j} = "";
end
end
fig = uifigure;
tab = uitable(fig);
opts = categorical({'1','2',''});
Data{2,1} = opts(3);
Column1 = Data(:,1);
Column2 = Data(:,2);
tab.Data = table(Column1,Column2);
By making the Columns editable and changing the <undefined> value it will show always one of the non-empty options entered in the categorical function and not allowing the cell to be empty (or <undefined>) anymore:
Is there a way to allow this types of cells to work like the drop downs works in Excel?
Thank you beforehand.

回答 (1 件)

Saurav
Saurav 2024 年 10 月 3 日
It seems there is a need to create dropdown cells in a “uitable” that can also accept empty values. However, there are some categorical limitations like that the categorical function does not support empty strings (" "), empty character vectors (''), or missing strings (<missing>) as category names. These are automatically converted to undefined categorical values unless explicitly categorized.
For further details, please refer to the documentation:
The following code provides a workaround:
% Create a cell array to hold data
Data = cell(21,2);
% Initialize all cells with a placeholder string
for i = 1:length(Data)
for j = 1:size(Data, 2)
Data{i,j} = "Select";
end
end
% Create a figure and a uitable
fig = uifigure;
tab = uitable(fig);
% Define the options for the drop-down including a placeholder for empty
opts = {'Select', '1', '2'};
% Convert the cell array to a string array
strData = string(Data);
% Convert columns to categorical arrays
Column1 = categorical(strData(:,1), opts, 'Ordinal', true);
Column2 = categorical(strData(:,2), opts, 'Ordinal', true);
% Assign the data to the uitable
tab.Data = table(Column1, Column2);
% Set the columns to be editable
tab.ColumnEditable = true;
% Function to update display data
function updateDisplayData(src)
data = src.Data;
for i = 1:height(data)
for j = 1:width(data)
if data{i, j} == categorical("Select")
data{i, j} = categorical("");
end
end
end
src.Data = data;
end
The code can be adjusted based on specific requirements regarding which cell values need dropdown functionality.
Hope that it helps.
  1 件のコメント
Antonio Anguita Vega
Antonio Anguita Vega 2024 年 10 月 3 日
Hi, thank you for your answer. Unfortunately the code does not work with the Ordinal set to true. I disabled it and works fine. It helps to know that I cant set the Columns to be always categorical allowing to set cells as <undefined> without losing the drop down functionality. I did some modifications to 'hide' the cells that are <undefined>:
% Create a cell array to hold data
Data = cell(21,2);
% Initialize all cells with a placeholder string
for i = 1:length(Data)
for j = 1:size(Data, 2)
Data{i,j} = "Select";
end
end
% Create a figure and a uitable
fig = uifigure;
tab = uitable(fig);
% Define the options for the drop-down including a placeholder for empty
opts = {'Select', '1', '2'};
% Convert the cell array to a string array
strData = string(Data);
% Convert columns to categorical arrays
Column1 = categorical(strData(:,1), opts, 'Ordinal', false);
Column2 = categorical(strData(:,2), opts, 'Ordinal', false);
% Assign the data to the uitable
tab.Data = table(Column1, Column2);
% Set the columns to be editable
tab.ColumnEditable = true;
% Function to update display data
updateDisplayData(tab);
[row,col] = find(ismissing(tab.Data));
s_odd = uistyle;
s_odd.FontColor = [1,1,1];
s_even = uistyle;
s_even.FontColor = [240/255,240/255,240/255];
row_even = row(mod(row,2) == 0);
col_even = col(mod(row,2) == 0);
row_odd = row(mod(row,2) ~= 0);
col_odd = col(mod(row,2) ~= 0);
addStyle(tab,s_odd,'cell',[row_odd,col_odd]);
addStyle(tab,s_even,'cell',[row_even,col_even]);
function updateDisplayData(src)
data = src.Data;
for i = 1:height(data)
for j = 1:width(data)
if data{i, j} == categorical("Select")
data{i, j} = "";
end
end
end
src.Data = data;
end
This will show something like this:
The only drawback is that whenever you select a cell the <undefined> will show a little bit, may be a change in font color within a SelectionChanged event can do the trick:
Is there a way to hide the word "Select" by changing the font color or something like that?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by