Inserting rows into uitable??
3 views (last 30 days)
Show older comments
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..
0 Comments
Accepted Answer
Walter Roberson
on 24 Dec 2013
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 Comments
Walter Roberson
on 25 Dec 2013
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
More Answers (0)
See Also
Categories
Find more on Whos 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!