フィルターのクリア

Storing values from edit text to uitable

1 回表示 (過去 30 日間)
Jose Alan Badillo Montes
Jose Alan Badillo Montes 2016 年 3 月 14 日
Hello, I have a problem with a guide, the thing that I want to do is everytime when I press the button, the value will be saved in the uitable. I did this with a for and it works, but the guide is giving me an array like it shown below, the array is always saving the last values. In the beginning of the guide, I declare z=0; that it is in global way.
0 0
0 0
0 0
0 0
10 20
function but1_Callback(hObject, eventdata, handles)
global z
a=get(handles.but1,'Value')
if a==1;
z=z+1;
s1=get(handles.edit1, 'String');
x1=str2num(s1);
s2=get(handles.edit2, 'String');
x2=str2num(s2);
res=x1+x2;
str=num2str(res);
set(handles.edit3, 'String', str)
data(z,:)=[x1 x2]
set(handles.uitable1,'data',data)
end
  2 件のコメント
Jan
Jan 2016 年 3 月 14 日
Avoid global variables. You have the handles struct, which is the perfrect location for storing a counter.
What does this mean:
I did this with a for and it works, but the guide is giving me
an array like it shown below
?
Jose Alan Badillo Montes
Jose Alan Badillo Montes 2016 年 3 月 15 日
Sorry for the mistake, I was referring to "for loop"

サインインしてコメントする。

採用された回答

Geoff Hayes
Geoff Hayes 2016 年 3 月 14 日
Jose - look closely at how you are updating data
data(z,:)=[x1 x2];
As you have indicated, z is a global variable (which as Jan points out, should be avoided) and you are using it to append the new x1 and x2 to the data in the uitable. This is fine, but you haven't defined what data is at this point! :) So this line of code will create a new array of z rows with the last being [*x1* x2] and all preceding rows are zero.
You first need to get the data from the table, then update it, and then save it back. Something like the following should work
data = get(handles.uitable1,'Data');
data = [data ; {x1} {x2}];
set(handles.uitable1,'Data',data);
Try the above and see what happens! (Note that the global z should no longer be necessary.)
  1 件のコメント
Jose Alan Badillo Montes
Jose Alan Badillo Montes 2016 年 3 月 15 日
Thank you so much, it works, you were right I didn't need the global variable "z".

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by