GUI can't read variable with setappdata and getappdata
Show older comments
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.
Accepted Answer
More Answers (1)
Yao Li
on 16 May 2013
0 votes
- 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 Comments
Yao Li
on 16 May 2013
Why not setappdata under the function listbox1_CreateFcn()?
Yao Li
on 16 May 2013
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
on 16 May 2013
Yao Li
on 16 May 2013
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
on 16 May 2013
And what does 6(a), 3(b),etc. mean?
Indri Djon Hansemit
on 16 May 2013
Yao Li
on 16 May 2013
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
on 16 May 2013
Indri Djon Hansemit
on 16 May 2013
Indri Djon Hansemit
on 16 May 2013
Categories
Find more on Creating, Deleting, and Querying Graphics Objects 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!