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

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

Accepted Answer

Walter Roberson
Walter Roberson on 9 Jun 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 Comments
Ingrid
Ingrid on 15 Jun 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

Sign in to comment.

More Answers (1)

Suchit Apage
Suchit Apage on 14 Jan 2017
U have to convert your data from double to cell Try this data=num2cell(data); For both add and remove
  1 Comment
Walter Roberson
Walter Roberson on 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.

Sign in to comment.

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!