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 日

0 投票

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 件のコメント

Animesh
Animesh 2013 年 12 月 24 日
Okay understood..But my name and Idno come from edit boxes..
Could you please help me with the necessary changes that must be made to the above code ? Thanks a lot in advance.
Walter Roberson
Walter Roberson 2013 年 12 月 24 日
編集済み: Walter Roberson 2013 年 12 月 24 日
dbt3 = uitable('Data',comp,'Position', [25 25 700 200]);
creates a new uitable, initializing it to whatever is in "comp", which you have not initialized (unless you have arranged this as a nested routine, which you probably did not do.) Or is "comp" what you load()'d ?
There is no indication that you have save()'d the changed dbt3 data after you added to it.
Why are you creating a new uitable each time, instead of creating the uitable once and set()'ing the Data parameter as appropriate? If the uitable is to be invisible sometimes then you can set() 'Visible' 'off' on it.
Animesh
Animesh 2013 年 12 月 25 日
No the new uitable is created only 1st time after that in the else block the uitable with all the saved data is created..This is the new code..
if true
% code
end
name=get(handles.edit1,'String');
Idno=get(handles.edit2,'String');
g=exist('C:\Users\Animesh\Documents\MATLAB\dbt7.mat')%ckecks for the existence of table
if g==0
comp=[];
dbt7 = uitable('Data',comp,'Position', [25 25 700 200]);
Exist=get(dbt7, 'Data');
k=0;
else
load('C:\Users\Animesh\Documents\MATLAB\dbt7.mat')
dbt7 = uitable('Data',comp,'Position', [25 25 700 200]);
Exist=get(dbt7, 'Data');
k=size(Exist,1)+1;
end
New={name,Idno}
comp=[Exist;New]
set(dbt7, 'Data',comp);
save('dbt7')
I have observed that if I initialize the name and idno into different variables in the first two lines I get a successful result..So my question is how should i initialize name and idno each time from different boxes.
Or is there a different way by clearing some variables??? Please help...
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.
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 件)

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

タグ

質問済み:

2013 年 12 月 24 日

コメント済み:

2013 年 12 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by