How can I add and remove a row from a Table in the GUI

5 vues (au cours des 30 derniers jours)
Paul Nanfah
Paul Nanfah le 9 Juin 2015
Commenté : Walter Roberson le 16 Jan 2017
Hey everybody,
I am working on project in matlab, and i first have to add and remove a row from a table in the GUI
after contructing the different items to the GUI , I wrote the code the following way:
% --- Executes just before untitled is made visible.
function untitled_OpeningFcn(hObject, eventdata, handles, varargin)
% Choose default command line output for untitled
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% --- Outputs from this function are returned to the command line.
function varargout = untitled_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in add.
function add_Callback(hObject, eventdata, handles)
data = get(handles.uitable1, 'data');
data(end+1,:) = 0;
set(handles.uitable1, 'data', data);
% --- Executes on button press in remove.
function remove_Callback(hObject, eventdata, handles)
data = get(handles.uitable1, 'data');
data(end-1,:);
set(handles.uitable1, 'data', data);
but it is somehow not working, the following error statment show up when I click on the button "Add" and the button "remove" does not respond at all!
What I am doing wrong ?
As you might have notice, I derived the code to "remove" from the one to "add", is that the right way to do ?
thank you in advance,
Paul

Réponse acceptée

Walter Roberson
Walter Roberson le 9 Juin 2015
You have coded the add callback under the assumption that when you fetch the data from the uitable that you will get back a numeric matrix. Numeric matrices are one of the two ways that you can store the data for a uitable, with the other way being cell array (one cell entry for each table entry). You need to know which way your empty table has been initialized, [] or {}. Or you could find out which one you currently have and make the conversion:
data = get(handles.uitable1, 'data');
if iscell(data); data = cell2mat(data); end
data(end+1,:) = 0;
set(handles.uitable1, 'data', data);
Your remove button is responding and doing what you programmed it to do. You have
data = get(handles.uitable1, 'data');
data(end-1,:);
set(handles.uitable1, 'data', data);
The second line of that,
data(end-1,:);
requests that the data in the second-last row be fetched into the current expression. And then the semi-colon at the end of the line says to do nothing with the current expression: if there were no semi-colon there then the result would be to display the current expression. Notice that you do not change your data before storing it back. Hint:
data(K,:) = []; %delete row K
  5 commentaires
Ingrid
Ingrid le 15 Juin 2015
function remove_Callback(hObject, eventdata, handles)
data = get(handles.uitable2, 'data');
data(end-1,:) = [];
set(handles.uitable2, 'data', data)
as was already previously advised by Walter in the second part of his first answer
Paul Nanfah
Paul Nanfah le 15 Juin 2015
thanks !

Connectez-vous pour commenter.

Plus de réponses (1)

Suchit Apage
Suchit Apage le 14 Jan 2017
U have to convert your data from double to cell Try this data=num2cell(data); For both add and remove
  1 commentaire
Walter Roberson
Walter Roberson le 16 Jan 2017
There are two approaches to this problem: convert the cell to numeric, or convert the numeric to cell. My personal preference is to work with cell arrays for uitable, as cell arrays are more flexible about mixing data types -- but I do admit that for fast-and-dirty programs I am more likely to convert the cell to numeric.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by