GUI can't read variable with setappdata and getappdata

1 回表示 (過去 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 Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by