How can I have 'dropdown' in an individual cell of a table in App Designer ?
조회 수: 59 (최근 30일)
이전 댓글 표시
Hello,
with reference to this previous resolved question, I would like to apply a drop down list to a single cell of the table and not to the whole column.
Is it possible by using table or can you suggest a better implementation?
app.UITable.Data = {'female';'male';'male'}; % have 3 rows of gender information.
app.UITable.ColumnFormat = {{'male', 'female'}}; % have option cell array within ColumnFormat property to indicate the 1st column is using dropdown menus.
app.UITable.ColumnEditable = true;
댓글 수: 0
채택된 답변
gaoyi guo
2022년 2월 24일
I have solved it perfectly! Please adopt my answer if it is helpful!
My version is 2021a.
I used the 'categorical' function:
fig = uifigure;
a = categorical({'Blue','Red'}); % define a categorical type of variable a
col1 = {a(1);200}; % the recipe is quote an element in a!!!!
col2 = [400;'x'];
tdata = table(col1,col2); % define a table using the above columns
uit = uitable(fig,'Data',tdata,'ColumnEditable',true);
댓글 수: 1
xiao yu
2023년 4월 9일
편집: xiao yu
2023년 4월 9일
Hi,
I am inspired by your approach and added a callback function.
Once a value changed in the first column, the corresponding row of the second column (not the whole column) will be changed to the corresponding category.
fig = uifigure;
channelNames=cell(10,1);
LabelNames=cell(10,1);
for i=1:10
if i<10
channelNames{i}=['channel 10' num2str(i)];
LabelNames{i}=['channel 10' num2str(i)];
else
channelNames{i}=['channel 1' num2str(i)];
LabelNames{i}=['channel 1' num2str(i)];
end
end
Meas={'Voltage';'Thermocoupler'};
TypesV= {'DC';'AC'};
TypesT= {'J';'Y'};
Meas=categorical(Meas);
TypesV=categorical(TypesV);
TypesT=categorical(TypesT);
Meas1={Meas(1);Meas(1);Meas(1);Meas(1);Meas(1);Meas(1);Meas(1);Meas(1);Meas(1);Meas(1)};
Type1={TypesV(1);TypesV(1);TypesV(1);TypesV(1);TypesV(1);TypesV(1);TypesV(1);TypesV(1);TypesV(1);TypesV(1)};
tdata = table(Meas1,Type1,LabelNames,'VariableNames',{'Mea','Type','Label'}); % define a table using the above columns
uit = uitable(fig,'Data',tdata,'ColumnEditable',true,...
'CellEditCallback',@(hObject,event)updateTable(hObject,event,TypesV,TypesT));
uit.UserData=tdata;
%%
function updateTable(hObject,event,TypesV,TypesT)
idx=event.Indices;
newData=event.NewData;
olddata=hObject.UserData;
tdata=hObject.Data;
row=idx(1);
col=idx(2);
if olddata.Mea{row}~=string(newData) && col==1
% consider only changes in the first column
if contains(string(newData),'Voltage')
tdata.Type{row}=TypesV(1);
elseif contains(string(newData),'Thermocoupler')
tdata.Type{row}=TypesT(1);
end
hObject.Data=tdata;
hObject.UserData=tdata;
end
end
추가 답변 (1개)
Sahithi Kanumarlapudi
2021년 2월 23일
Hi,
'uidropdown' is normally used to create a dropdown in MATLAB. But, it's parent can only be 'Figure object (default) | Panel object | Tab object | ButtonGroup object | GridLayout object' as per the documentation here as of now.
Hope this helps!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!