Inserting rows into uitable??

조회 수: 3 (최근 30일)
Animesh
Animesh 2013년 12월 24일
댓글: Animesh 2013년 12월 25일
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에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by