MATLAB: How to define each case of the choice list in a uitable (GUI) and put IF function for each one

choice list tableMATLAB

I made a matlab gui that have two uitables, one of them have choice list format on its cells but I dont know how define each case of choice list and put IF function for each one. In other words I want apply numbers to second uitable from another gui with dependency on cases in the choice list of first uitable.

Best Answer

Hi Omid,
You can try below this code I have written some commands on it.
classdef myuitable < handle
properties
guiHandles
table1_data
table2_data
selectedRows
selectedColumns
end
methods
function obj = myuitable
colNames = {'Load1','Load2','Load3'};
rowNames = {'XLC01','XLC02','XLC03','XLC04','XLC05'};
colFormat1 = {'Selection1','Selection2','Selection3','Selection4'};
colFormat2 = {'1','2','3'};
C = cell(5,1);
C(:) = {'NaN'};
table1Data = [C,num2cell(nan(5,2))];
table2Data = num2cell(zeros(5,3));
obj.guiHandles.mainGUI = figure(...
'units' , 'normalized',...
'Position' , [0.2 0.2 0.65 0.5],...
'NumberTitle', 'off',...
'Name' , 'My uitable App',...
'Toolbar' , 'none',...
'MenuBar' , 'none' ...
);
obj.guiHandles.table1 = uitable(...
'Parent' , obj.guiHandles.mainGUI,...
'units' ,'normalized',...
'Position' , [0,0.7,0.3,0.3],...
'ColumnName' , colNames,...
'RowName' , rowNames,...
'Data' , table1Data,...
'ColumnEditable' ,[true,true,false],...
'ColumnFormat' ,{colFormat1,colFormat2},...
'CellEditCallback' , @obj.table1CellEdited,...
'CellSelectionCallback' , @obj.table1CellSelected...
);
obj.guiHandles.table2 = uitable(...
'Parent' , obj.guiHandles.mainGUI,...
'units' ,'normalized',...
'Position' , [0.5,0.7,0.3,0.3],...
'ColumnName', colNames,...
'RowName' , rowNames,...
'Data' , table2Data...
);
obj.table1_data = table1Data;
obj.table2_data = table2Data;
end
function table1CellEdited(handleObj,source,eventdata) % First three input explained in cell selected callback
% you can either use varargin, or three different inputs like
% above.
%%small check for cell selection works correctly
selectedIndicesSavedInObjectProperty = [handleObj.selectedRows, handleObj.selectedColumns];
selectedIndicesInEventData = eventdata.Indices;
if isequal(selectedIndicesSavedInObjectProperty,selectedIndicesInEventData)
disp('Indices are the same');
else
disp('Indices are not the same');
end
%%Now we can create some cases for selections
if handleObj.selectedColumns == 1 % if your selected column is first column and any data changed here
userSelection = eventdata.EditData;
currentTable2Data = handleObj.guiHandles.table2.Data;
switch userSelection
case 'Selection1'
currentTable2Data(handleObj.selectedRows,handleObj.selectedColumns) = cellstr(eventdata.NewData);
case 'Selection2'
currentTable2Data(handleObj.selectedRows,handleObj.selectedColumns) = cellstr(eventdata.NewData);
case 'Selection3'
currentTable2Data(handleObj.selectedRows,handleObj.selectedColumns) = cellstr(eventdata.NewData);
case 'Selection4'
currentTable2Data(handleObj.selectedRows,handleObj.selectedColumns) = cellstr(eventdata.NewData);
end
newTable2Data = currentTable2Data;
handleObj.guiHandles.table2.Data = newTable2Data;
elseif handleObj.selectedColumns == 2 % if your selected column is second column and any data changed here
userSelection = eventdata.EditData;
currentTable2Data = handleObj.guiHandles.table2.Data;
switch userSelection
case '1'
currentTable2Data(handleObj.selectedRows,3) = cellstr(eventdata.EditData); % You can define any value to any cell here. I have chosen to change 3rd column of second table
case '2'
currentTable2Data(handleObj.selectedRows,3) = cellstr(eventdata.EditData);
case '3'
currentTable2Data(handleObj.selectedRows,3) = cellstr(eventdata.EditData);
end
newTable2Data = currentTable2Data;
handleObj.guiHandles.table2.Data = newTable2Data;
else
% Do nothing
end
end
function table1CellSelected(varargin) % you will always get 3 inputs for the callback functions automatically
% when mouse clicked to any cell on your uitable this function
% automatically called by your main class.
handleObj = varargin{1}; % first input is always an instance of your class object (myuitable)
source = varargin{2}; % second input is always an instance of the GUI item(uicontrol,uiable) object (eg. a table, a button, etc...)
eventdata = varargin{3}; % third input is wlways eventdata object whic represents what happened to which source
% eventdata gives you the details about click event(eg.Indices)
handleObj.selectedRows = eventdata.Indices(1); % eventdata.Indices gives you a [x y], first value row number, second is column number.
handleObj.selectedColumns = eventdata.Indices(2);
% now you saved the selected information in your object
% property. We will use this when data changed in your table.
end
end
end