GUI can't read variable with setappdata and getappdata
    9 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
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.
0 Commenti
Risposta accettata
  Yao Li
      
 il 16 Mag 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 Commenti
  Yao Li
      
 il 17 Mag 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.
Più risposte (1)
  Yao Li
      
 il 16 Mag 2013
        - 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 Commenti
Vedere anche
Categorie
				Scopri di più su Creating, Deleting, and Querying Graphics Objects in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

