Hi everybody,
once the row of the table has been selected with the mouse I would like to delete it with the delet row button.
When I push delet button I can't select the Indices. Matlab shows me the following answer:
Index =
1×0 empty double row vector
Struct contents reference from a non-struct array object.
The following code:
function varargout = tabella(varargin)
% TABELLA MATLAB code for tabella.fig
% TABELLA, by itself, creates a new TABELLA or raises the existing
% singleton*.
%
% H = TABELLA returns the handle to a new TABELLA or the handle to
% the existing singleton*.
%
% TABELLA('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TABELLA.M with the given input arguments.
%
% TABELLA('Property','Value',...) creates a new TABELLA or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before tabella_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to tabella_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help tabella
% Last Modified by GUIDE v2.5 15-Dec-2018 17:52:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @tabella_OpeningFcn, ...
'gui_OutputFcn', @tabella_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before tabella is made visible.
function tabella_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to tabella (see VARARGIN)
% Choose default command line output for tabella
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes tabella wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = tabella_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in add_row.
function add_row_Callback(hObject, eventdata, handles)
% hObject handle to add_row (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
data=get(handles.uitable4, 'data');
data(end+1,:)={''};
set(handles.uitable4, 'data', data);
% --- Executes on button press in delete_row.
function delete_row_Callback(hObject, eventdata, handles)
% hObject handle to delete_row (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
set(handles.uitable4,'CellSelectionCallBack',@(h,e) set(h,'UserData',e))
D=get(handles.uitable4,'Data');
Index=get(handles.uitable4,'UserData')
D(Index.Indices(:,1), :) = [];
set(handles.uitable4,'Data',D);

 채택된 답변

Adam Danz
Adam Danz 2018년 12월 15일

1 개 추천

The UserData does not contain the index of the cell selection. To get the index of your cell selection you need to use the 2nd input to the callback function. See below.
Assuming delete_row_Callback() is a cell selection callback function, hObject is the first input, eventdata is the second input,
D=hObject.Data;
Index=eventdata.Indices;
D(Index(1), :) = [];
hObject.Data = D;

댓글 수: 11

Thank you for your answer however it doesn't work!
function delete_row_Callback(hObject, eventdata, handles)
D=hObject.Data;
Index=eventdata.Indices;
D(Index(1), :) = [];
hObject.Data = D;
It shows me the following error:
No appropriate method, property, or field 'Data' for class 'matlab.ui.control.UIControl'.
Error in tabella>delete_row_Callback (line 99)
D=hObject.Data;
Adam Danz
Adam Danz 2018년 12월 17일
편집: Adam Danz 2018년 12월 17일
I tested the solution in matlab 2018a and it does work. What version of matlab are you using?
Perhaps your delete_row_Callback() function is not a cell selection callback function or any other callback function associated with your table. The code I provided should go in your cell selection callback function or your cell edit callback function. Confirm that this is the case.
dario cecchetti
dario cecchetti 2018년 12월 17일
편집: dario cecchetti 2018년 12월 17일
Yes, You're right! My function was not cell selection callback. I have rewritte the code like you (the row is delected), but Matlab shows me the following error:
Index exceedes matrix dimensions
I would also like to delete the selected row with the delete_row button
% --- Executes on button press in delete_row.
function delete_row_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes when selected cell(s) is changed in uitable2.
function uitable2_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable2 (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)
D=hObject.Data;
Index=eventdata.Indices;
D(Index(1),:) = [];
hObject.Data = D;
thank you for your availability
Adam Danz
Adam Danz 2018년 12월 17일
Could you provide the values of the following?
  • D
  • Index
Below, the value of Index and D.
Thank you! :)
D =
6×2 cell array
'' ''
'' ''
'' ''
'' ''
'' ''
'' ''
Index =
3 1
D =
5×2 cell array
'' ''
'' ''
'' ''
'' ''
'' ''
hObject =
Table (uitable2) with properties:
Data: {5×2 cell}
ColumnWidth: {'auto' 'auto' 'auto'}
ColumnEditable: [1 1 1]
CellEditCallback: ''
Position: [34.8000 7.4615 55.2000 15.6154]
Units: 'characters'
Show all properties
D =
5×2 cell array
'' ''
'' ''
'' ''
'' ''
'' ''
Index =
0×2 empty double matrix
Index exceeds matrix dimensions.
Error in prova>uitable2_CellSelectionCallback (line 92)
D(Index(1),:) = []
Error in gui_mainfcn (line 95)
feval(varargin{:});
Error in prova (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)prova('uitable2_CellSelectionCallback',hObject,eventdata,guidata(hObject))
Error while evaluating Table CellSelectionCallback.
It looks like the row is correctly being deleted but then you get the indexing error due to an empty index. I'm not sure what's causing the empty index but you could introduce a conditional that stops the callback function if the index is empty.
At the beginning of your callback funciton,
if isempty(eventdata.Indices)
return
end
Thanks, it works perfectly! Now I would like to pass the index values ​​to the delete_row_callback () function (it is not a cell selection callback) and to delete the row by clicking on the delete button.
I can not pass the Index values ​​to the delete_row_callback function.
thanks a lot.
function delete_row_Callback(hObject, eventdata, handles)
% hObject handle to delete_row (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
Index=@uitable2_CellSelectionCallback
if isempty(eventdata.Indices)
return
end
D(Index(1),:) = []
hObject.Data = D
% --- Executes when selected cell(s) is changed in uitable2.
function [Index]=uitable2_CellSelectionCallback(hObject, eventdata, handles)
% hObject handle to uitable2 (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)
if isempty(eventdata.Indices)
return
end
D=hObject.Data;
Index=eventdata.Indices
% D(Index(1),:) = [];
% hObject.Data = D
Adam Danz
Adam Danz 2018년 12월 17일
편집: Adam Danz 2018년 12월 17일
This is a different solution and could have been more clearly stated earlier.
The cell selection callback should merely store the most recent cell selection in UserData.
function [Index]=uitable2_CellSelectionCallback(hObject, eventdata, handles)
if isempty(eventdata.Indices)
return
end
hObject.UserData = eventdata.Indices;
end
When your button is pressed it will activate your delete....() function which will then access the UI table. In this example, your UItable handle is "UItable".
function delete_row_Callback(hObject, eventdata, handles)
Index = handles.UItable.UserData; %I don't know what your UI table handle is.
Data = handles.UItable.Data;
% now remove the rows and reasign the data.....
end
dario cecchetti
dario cecchetti 2018년 12월 17일
it works perfectly! thanks for your patience!!! :)
Adam Danz
Adam Danz 2018년 12월 17일
Nice! Glad it worked out.
Cristian Martin
Cristian Martin 2022년 5월 23일
D=hObject.Data;
Index=eventdata.Indices;
D(Index(1), :) = [];
hObject.Data = D;
How can I make this job works from a pushbutton?

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

추가 답변 (0개)

카테고리

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

질문:

2018년 12월 15일

댓글:

2022년 5월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by