Why is my UITABLE not refreshing in MATLAB 7.6 (R2008a) when I use KeyPressFcn?

2 views (last 30 days)
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.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
In certain cases, the UITABLE does not refresh right after its 'Data' property is set. To work around this issue, put a call to the DRAWNOW function at the beginning of the KeyPressFcn.
if(~strcmp(eventdata.Key,'return')), return; end
%put a DRAWNOW statement
drawnow
%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
DRAWNOW flushes the event queue and updates figure window. For more information about the DRAWNOW function, please refer to the documentation by executing the following in the MATLAB command line:
doc drawnow

More Answers (0)

MathWorks Support

Categories

Find more on Environment and Settings in Help Center and File Exchange

Products


Release

R2008a

Community Treasure Hunt

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

Start Hunting!