Inserting rows into uitable??

1 次查看(过去 30 天)
I have a simple gui containing two edit boxes and a push button.On pressing the push button the values of the edit boxes are inserted into a uitable.The code for pushback button callback is:
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
name=get(handles.edit1,'String');%the value of the first edit box
Idno=get(handles.edit2,'String');%the value of the second edit box
g=exist('C:\Users\Animesh\Documents\MATLAB\dbt3.mat')%ckecks for the existence of table
if g==0
comp=[];
dbt3 = uitable('Data',comp,'Position', [25 25 700 200]);
Exist=get(dbt3, 'Data');
New={name,Idno};
comp=[Exist;New]
set(dbt3, 'Data',comp);
save('dbt3')
else
load('C:\Users\Animesh\Documents\MATLAB\dbt3.mat')
dbt3 = uitable('Data',comp,'Position', [25 25 700 200]);
Exist=get(dbt3, 'Data');
New={name,Idno};
comp=[Exist;New]
set(dbt3, 'Data',comp);
save('dbt3')
end
The problem is that when I run it for the first time the control is in the if block and the first row is added into the uitable.But when i run the 2nd time the control goes to the else block but the next row added is the same as the previous row.. Can anyone help?? Thanks a lot in advance..

采纳的回答

Walter Roberson
Walter Roberson 2013-12-24
Why would it be different() ? When you load(), you are not affecting "name" or "Idno". Unless, that is, one of those happens to be the name of a variable stored in the .mat file, which is something I would not assume.
It is poor programming practice to rely on load() overwriting variables in the workspace. It is better to use the form of load() that assigns to a variable, and pull the appropriate data out of the variable.
InData = load('C:\Users\Animesh\Documents\MATLAB\dbt3.mat');
Idno = InData.Idno;
...
  6 个评论
Walter Roberson
Walter Roberson 2013-12-25
Every call to uitable creates a new uitable object and does not remove the old object. The old object might be covered over by the new object.
You should be using load() to return a structure, and you should be specific about what you are save()'ing.
I don't think you should be using load() / save() here at all. See
Animesh
Animesh 2013-12-25
Thanks a lot sir for helping me throughout..I got my error.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Migrate GUIDE Apps 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by