GUI can't read variable with setappdata and getappdata

1 vue (au cours des 30 derniers jours)
Indri Djon Hansemit
Indri Djon Hansemit le 16 Mai 2013
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.

Réponse acceptée

Yao Li
Yao Li le 16 Mai 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,handles);
c=getappdata(handles.listbox1,'c2');
set(handles.listbox1,'String',c);
function hitung(handles_hitung,handles)
a=str2num(get(handles.edit1,'String'));
b=str2num(get(handles.edit2,'String'));
d=str2num(get(handles.edit3,'String'));
c=a/b;
while c>d
a=a-1;
c=a/b;
end
setappdata(handles_hitung,'c2',c);
% --- Executes on selection change in listbox1.
function listbox1_Callback(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns listbox1 contents as cell array
% contents{get(hObject,'Value')} returns selected item from listbox1
% --- Executes during object creation, after setting all properties.
function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',6)
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',3)
function edit3_Callback(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit3 as text
% str2double(get(hObject,'String')) returns contents of edit3 as a double
% --- Executes during object creation, after setting all properties.
function edit3_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',1)
  2 commentaires
Indri Djon Hansemit
Indri Djon Hansemit le 16 Mai 2013
thank you for your answer, finally I did it. I add some formula and 2 listbox to display the result of formula. how shoul I call in pushbutton1? is this the right :
hitung(handles.listbox1,handles.listbox2,handles);
Yao Li
Yao Li le 17 Mai 2013
I'm afraid not. Only 2 input arguments and the 2nd one must be handles. The 1st can be specified to any handles you want.

Connectez-vous pour commenter.

Plus de réponses (1)

Yao Li
Yao Li le 16 Mai 2013
  1. The handle for getappdata and setappdata must be the same
  2. If you write setappdata in the function hitung,you can get data by implementing getappdata only after the function hitung has been called
  10 commentaires
Indri Djon Hansemit
Indri Djon Hansemit le 16 Mai 2013
I want to display all the values of c, but I add one more listbox and formula, I confused how to call function hitung with 2 listboxs.
Indri Djon Hansemit
Indri Djon Hansemit le 16 Mai 2013
function pushbutton1_Callback(hObject, eventdata, handles)
hitung(handles.listbox1,handles.listbox2,handles);
c=getappdata(handles.listbox1,'c2');
e=getappdata(handles.listbox2,'e2');
set(handles.listbox1,'String',c);
set(handles.listbox2,'String',e);
% 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)
function hitung(handles_hitung,handles)
a=str2num(get(handles.edit1,'String'));
b=str2num(get(handles.edit2,'String'));
d=str2num(get(handles.edit3,'String'));
c=a/b;
e=a*b;
while c>d
a=a-1;
c=a/b;
e=a*b;
end
setappdata(handles_hitung,'c2',c);
setappdata(handles_hitung,'e2',e);

Connectez-vous pour commenter.

Catégories

En savoir plus sur Interactive Control and Callbacks dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by