GUI can't read variable with setappdata and getappdata
显示 更早的评论
I'm working on setappdata and getappdata. My program calls another function within function callback.
function pushbutton1_Callback(hObject, eventdata, handles)
hitung(handles)
d=str2num(get(handles.edit3,'String'));
c=getappdata(handles.pushbutton1,'c2');
set(handles.edit4,'String',c);
while c>d
a=a-1;
c=a/b;
set(handles.listbox1,'String',c);
end
function hitung(handles)
a=str2num(get(handles.edit1,'String'));
b=str2num(get(handles.edit2,'String'));
c=a/b;
set(handles.listbox1,'String',c);
setappdata(handles.listbox1,'c2',c);
I tried edit1 <6>, edit2 <3>, edit3 <1>. But variable c from <c=getappdata(handles.pushbutton1,'c2');> can't display in edit4, and because of that while loops can't run. So the result in listbox1 <2>. Is there any solution? Thankyou.
采纳的回答
更多回答(1 个)
Yao Li
2013-5-16
0 个投票
- The handle for getappdata and setappdata must be the same
- If you write setappdata in the function hitung,you can get data by implementing getappdata only after the function hitung has been called
10 个评论
Yao Li
2013-5-16
Why not setappdata under the function listbox1_CreateFcn()?
Yao Li
2013-5-16
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)
hitung(handles.listbox1);
c=getappdata(handles.listbox1,'c2')
function hitung(handles)
c=2;
setappdata(handles,'c2',c);
If you do need the function hitung, try the codes above. I have tested above codes which works well.
Indri Djon Hansemit
2013-5-16
Yao Li
2013-5-16
Indri, I just gave you an example not the exact codes what you want. However, I don't think the while loop will be a problem which is only used to set the value of c. If you do have problems in fixing this and also if it's possible, send them to me.
Yao Li
2013-5-16
And what does 6(a), 3(b),etc. mean?
Indri Djon Hansemit
2013-5-16
Yao Li
2013-5-16
Do u want the listbox only display the final value of c or all the values of a,b,c and d?
Indri Djon Hansemit
2013-5-16
Indri Djon Hansemit
2013-5-16
Indri Djon Hansemit
2013-5-16
类别
在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!