GUI can't read variable with setappdata and getappdata

조회 수: 2 (최근 30일)
Indri Djon Hansemit
Indri Djon Hansemit 2013년 5월 16일
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.

채택된 답변

Yao Li
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,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
Indri Djon Hansemit
Indri Djon Hansemit 2013년 5월 16일
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 2013년 5월 17일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Yao Li
Yao Li 2013년 5월 16일
  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
Indri Djon Hansemit
Indri Djon Hansemit 2013년 5월 16일
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 2013년 5월 16일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by