How can I add and remove a row from a Table in the GUI
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2015-6-9
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 个评论
Ingrid
2015-6-15
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
更多回答(1 个)
Suchit Apage
2017-1-14
U have to convert your data from double to cell Try this data=num2cell(data); For both add and remove
1 个评论
Walter Roberson
2017-1-16
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Logical 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!