GUI can't read variable with setappdata and getappdata

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.

 Accepted Answer

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 Comments

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);
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.

Sign in to comment.

More Answers (1)

  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 Comments

Why not setappdata under the function listbox1_CreateFcn()?
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.
I still don't understand about the codes above. My codes using while looping and you're not. I've replaced the handles in <c=getappdata(handles.pushbutton1,'c2');> with listbox1. And when I compiled it, my program is not responding (I used a=edit1 <6>, b=edit2 <3>). I wish my program will display the result like this :
edit1=6 (a)
edit2=3 (b)
edit3=1 (d)
and when I push the button :
listbox1=1
But it still not responding when I run it. Help me
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.
And what does 6(a), 3(b),etc. mean?
there are 3 inputs (edit1, edit2, edit3).
edit1 known as variable "a", edit2 as variable "b", and edit3 as variable "d".
I input in edit1 = 6 (variable a), edit2 = 3 (variable b), edit3 = 1 (variable d).
when I push the button, function hitung count c = a/b, c = 6/3 = 2, and then variable "c" compared with variable "d" using while.
while c > d, then value of "a" become a = a-1, a = 6-1 = 5.
count new value of variable "c" with a = 5, and this calculation c = a/b will repeat as long as c > d, and it will end if c < d. which is mean until a = 3.
but it still no responding when I run it. I don't understand. this is the error messages :
??? Attempt to execute SCRIPT loopingwhile as a function:
C:\Users\Toshiba\Documents\MATLAB\loopingwhile.m
Error in ==> @(hObject,eventdata)loopingwhile('edit5_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Attempt to execute SCRIPT loopingwhile as a function:
C:\Users\Toshiba\Documents\MATLAB\loopingwhile.m
Error in ==> @(hObject,eventdata)loopingwhile('edit4_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Attempt to execute SCRIPT loopingwhile as a function:
C:\Users\Toshiba\Documents\MATLAB\loopingwhile.m
Error in ==> @(hObject,eventdata)loopingwhile('edit3_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Attempt to execute SCRIPT loopingwhile as a function:
C:\Users\Toshiba\Documents\MATLAB\loopingwhile.m
Error in ==> @(hObject,eventdata)loopingwhile('listbox1_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Attempt to execute SCRIPT loopingwhile as a function:
C:\Users\Toshiba\Documents\MATLAB\loopingwhile.m
Error in ==> @(hObject,eventdata)loopingwhile('edit2_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
??? Attempt to execute SCRIPT loopingwhile as a function:
C:\Users\Toshiba\Documents\MATLAB\loopingwhile.m
Error in ==> @(hObject,eventdata)loopingwhile('edit1_CreateFcn',hObject,eventdata,guidata(hObject))
??? Error using ==> struct2handle
Error while evaluating uicontrol CreateFcn
Do u want the listbox only display the final value of c or all the values of a,b,c and d?
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);
and how to make the iteration of looping display in that listbox?thankyou so much for help.
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.
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);

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!