GUI can't read variable with setappdata and getappdata
Mostrar comentarios más antiguos
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.
Respuesta aceptada
Más respuestas (1)
Yao Li
el 16 de Mayo de 2013
0 votos
- 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 comentarios
Yao Li
el 16 de Mayo de 2013
Why not setappdata under the function listbox1_CreateFcn()?
Yao Li
el 16 de Mayo de 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
el 16 de Mayo de 2013
Yao Li
el 16 de Mayo de 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
el 16 de Mayo de 2013
And what does 6(a), 3(b),etc. mean?
Indri Djon Hansemit
el 16 de Mayo de 2013
Yao Li
el 16 de Mayo de 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
el 16 de Mayo de 2013
Indri Djon Hansemit
el 16 de Mayo de 2013
Indri Djon Hansemit
el 16 de Mayo de 2013
Categorías
Más información sobre Creating, Deleting, and Querying Graphics Objects en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!