I have created a UITABLE with 3 columns and 1 row using GUIDE and the following statement in the CreateFcn of the UITABLE:
set(hObject, 'Data', cell(1,3));set(hObject, 'ColumnName', {'1st number', '2nd number', 'SUM'},'ColumnEditable', [true true true]);
I would like the third column to always be the sum of the first two columns. My GUI should function as follows: given 2 numbers in the first 2 columns it should compute the sum of the 2 numbers and put it in the 3rd column if the Enter key is pressed. If there is already a number in the third column and it is not equal to the sum of the numbers in the previous columns I would like the number in the 3rd column to change. To accomplish that, I put the following code into the KeyPressFcn for the UITABLE:
if(~strcmp(eventdata.Key,'return')), return; end%convert the cell array data in the uitable to a matrix
data = cell2mat(get(handles.uitable1,'Data'));if size(data,2)==2 || data(1)+data(2)~=data(3) data(3)=data(1)+data(2); set(handles.uitable1,'Data',data);end
The GUI however, does not work correctly when there is already a number in the 3rd column. The UITABLE does not refresh as expected.
Best Answer