I have a uitable that I want to be able to select a cell with a mouse, and the value in that cell be put in an edit box. Under the CellSelectionCallback, I have the following code.
However, it attempts to run (and crash) when the contents of the uitable change. How can I only implement this when the mouse selects that cell?
function uitable1_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable1 (see GCBO)
% eventdata structure with the following fields (see MATLAB.UI.CONTROL.TABLE)
% Indices: row and column indices of the cell(s) currently selecteds
% handles structure with handles and user data (see GUIDATA)
%handles=guidata(source);
handles.currentCell=eventdata.Indices;
guidata(gcf,handles);
handles=guidata(gcf);
Indices=handles.currentCell;
data=get(handles.uitable1,'Data');
data=data(Indices(1),Indices(2));
set(handles.editNum,'String', data);

댓글 수: 5

Jason - why is the code crashing? What is the error message? Put a breakpoint at the line
handles.currentCell=eventdata.Indices;
and observe (i.e. step through the code to see) what happens when the contents of the eatable changes.
The crash could be related to eventdata.Indices being empty and so the calls to Indices(1) and Indices(2) produce the errors. Guard against this case by doing something like
if ~isempty(eventdata.Indices)
handles.currentCell=eventdata.Indices;
guidata(hObject,handles);
Indices=handles.currentCell;
data=get(handles.uitable1,'Data');
data=data(Indices(1),Indices(2));
set(handles.editNum,'String', data);
end
Note the call to handles=guidata(gcf); is unnecessary and so can be removed, and the switch from gcf to hObject (see the documentation of guidata to understand why this is possible).
Oh I see, I thought the function was more like on "onclick" callback. It seems once you have selected a cell it forever watches that for a change. As the function uses the idnex of the cell to refer back to the underlying "data" that was used, when it changes, there are occurances when this is empty and causes an error:
Attempted to access data(1,1); index out of bounds because size(data)=[0,0].
Error in OpenFile>uitable1_CellSelectionCallback (line 1282)
data=data(Indices(1),Indices(2));
Is there a way to switch off this monitoring of the selected cell once it has updated the edit box ("editNum"). This will prevent all future errors??
Geoff Hayes
Geoff Hayes 2014년 12월 19일
Given your error message, I don't understand why there is no data. Is it because you have deleted all elements from the table?
A problem with the code may be because the selected cell retains focus and so you may have to shift the focus to some other control, or just guard against the problem you are experiencing.
Thankyou Geoff, I've included another check before executing this:
if ~isempty(eventdata.Indices)&&~isempty(data)
Geoff Hayes
Geoff Hayes 2014년 12월 19일
Cool, Jason. Consider making your solution (in the above comment) as an answer to your question.

댓글을 달려면 로그인하십시오.

 채택된 답변

Jason
Jason 2014년 12월 22일
편집: Jason 2014년 12월 22일

0 개 추천

Thankyou Geoff for the help, I've included another check before executing this:
if ~isempty(eventdata.Indices)&&~isempty(data)
if ~isempty(eventdata.Indices)
handles.currentCell=eventdata.Indices;
guidata(hObject,handles);
Indices=handles.currentCell;
data=get(handles.uitable1,'Data');
data=data(Indices(1),Indices(2));
set(handles.editNum,'String', data);
end

댓글 수: 1

this method does work, but only to display one cell only. can you help me how to display one table row, by just clicking. Please help me :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 App Building에 대해 자세히 알아보기

질문:

2014년 12월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by