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)
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
if handleObj.selectedColumns == 1
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
userSelection = eventdata.EditData;
currentTable2Data = handleObj.guiHandles.table2.Data;
switch userSelection
case '1'
currentTable2Data(handleObj.selectedRows,3) = cellstr(eventdata.EditData);
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
end
end
function table1CellSelected(varargin)
handleObj = varargin{1};
source = varargin{2};
eventdata = varargin{3};
handleObj.selectedRows = eventdata.Indices(1);
handleObj.selectedColumns = eventdata.Indices(2);
end
end
end
Best Answer