Writing Callback For GUI

Hello Everyone ! I am experiencing a problem while writing callback for pushbutton in GUI. The problem is that the function that ,is calculated in the callback , is represented by other 5-6 functions ,which also depend on user inputs ( also from GUI) . I would be extremely grateful if anyone could give a piece of advice on how to implement it in Matlab. Thanks in advance! Best Regards

댓글 수: 2

Jarrod Rivituso
Jarrod Rivituso 2011년 4월 5일
Are you getting any errors when trying to call these other functions from within your callback? If so, are the other functions in separate MATLAB files? Are they on your MATLAB path?
Dimitry
Dimitry 2011년 4월 6일
Hi !
If i put these functions in pushbutton callback , the pushbutton simply does not work. I tried to place them separately in the callback , then I got mistake since matlab was not able to obtain input values for the functions.I have all functions in separate files in Matlab path. The program works perfectly when I just put some fixed values for the parameters user should input, so I just wonder how one can make matlab read the input values send them to functions. Because,as said before all function depend pretty much on the same values.
Regards

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

 채택된 답변

Dimitry
Dimitry 2011년 4월 12일

0 개 추천

Hi again ! I tried to use function assignin and getappdata to send handles value to the my own function Funk, which is stored in separate matlab file. However, the problem is that Matlab does not recognize the handles I am referring to . Every time , I get the following message :
??? Undefined variable "handles" or class "handles.iter".
Error in ==> Funk at 2 M1=getappdata(handles.iter)
Error in ==> thesisgui>pushbuttonKim_Callback at 280 while (abs(Funk(u,n))>eps) && (w>1) && (n>1)
I wonder if someone could give a piece of advice on this one. I am really stuck with it (=

댓글 수: 2

Walter Roberson
Walter Roberson 2011년 4월 12일
The first parameter to getappdata() must be the handle of the object to retrieve the data from. Unfortunately for you, you do not pass an object handle down to that routine. You might, however, be able to refer to the pushbutton by calling gcbo
M1=getappdata(gcbo, handles.iter)
But! The second parameter to getappdata() needs to be the item *name*, and handles.iter is not an item *name* (especially with it being undefined.)
I would suggest,
handles = guidata(gcbo);
I suspect you will find that you do not need to use getappdata or assignin .
Dimitry
Dimitry 2011년 4월 12일
Thank you very much Walter Roberson !!!=) Now it works perfectly , I did not even need to use getappdata , it was enough with ´handles = guidata(gcbo) and then with simply get.

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

추가 답변 (3개)

Matt Fig
Matt Fig 2011년 4월 6일

0 개 추천

Where would you get the values for the functions? If you are wanting to get them from other uicontrols (editboxes, for example), then you must query the correct property of the uicontrols from within the pushbutton callback.
Tell where you would like to get the inputs to the separate functions...

댓글 수: 4

Dimitry
Dimitry 2011년 4월 6일
Thank you for your answer. I need to get the inputs from editboxes .I wonder if you could explain which commands I need to implement in pushbutton's callback in order to succeed with this task .Another thing that is really puzzling me is where I should have the functions in separate files or in some part of the GUI callback.
Thanks in advance !
Regards
Matt Fig
Matt Fig 2011년 4월 7일
In the callback to the pushbutton, you simply query the 'string' property of the editboxes. If you are expecting numeric values, remember to convert them first.
N1 = str2double(get(editbox1handle,'string'));
N2 = str2double(get(editbox2handle,'string'));
N3 = str2double(get(editbox3handle,'string'));
...etc
The handles to the editboxes can be obtained through the handles structure, or by calling GUIDATA or GUIHANDLES.
Dimitry
Dimitry 2011년 4월 8일
Hi !
Thank you for your advice. I implemented but I still got the same error. I wonder where I should place the functions that depend on these variables and the function that is executed in the callback also depends on them .
Thanks in advance for your help
Matt Fig
Matt Fig 2011년 4월 8일
The more information I have, the better I can help you. So:
WHAT ERROR?
Please show the entire error message!
Also, what do you mean "the function that also depends on them?" You need to be a lot clearer in what you are trying to do. It was my understanding that you had several M-file functions in different files which you wanted to run when a pushbutton was pressed. Further, the arguments to these function M-files were to be taken from the editboxes in the GUI. So they are the functions that depend on these values, so what do you mean by "function that is executed in the callback also depends on them . "
Please, if you want help, be VERY clear about what is supposed to happen when the button is pushed.

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

Dimitry
Dimitry 2011년 4월 6일

0 개 추천

Thank you for your answer. I need to get the inputs from editboxes .I wonder if you could explain which commands I need to implement in pushbutton's callback in order to succeed with this task .Another thing that is really puzzling me is where I should have the functions in separate files or in some part of the GUI callback. Thanks in advance ! Regards
Dimitry
Dimitry 2011년 4월 8일

0 개 추천

Sorry that I was not perfectly clear. I have the following code which I want to execute when the pushbutton is pressed :
w=M;
n=M;
u=ones(M,1)
delT=T/M;
eps=0.000000001;
u(w)=(K*min(1,r/delta));
while (abs(Funk(u,n))>eps) && (w>1) && (n>1)
u(w-1)=u(w)-(Funk(u,n)/Funkder(u,n));
n=n-1;
w=w-1;
end
u
u0=u(1)-(Funk(u,1))/(Funkder(u,1))
if (u0>S0 || u0==S0)
price=K-S0
else
for h=M:-1:3
summe(h)=(delT)/3*(2*f1(S0,u(h-2),(h-2)*delT) +4*f1(S0,u(h-1),(h-1)*delT)+f1(S0,u(h),h*delT));
end
summe1=sum(summe);
price=K*exp(-r*T)*normcdf(-(log(S0/K)+(r-delta-(sigma^0.5)/2)*T)/(sigma*T^0.5))-S0*exp(-sigma*T)*normcdf(-(log(S0/K)+(r-delta+(sigma^0.5)/2)*T)/(sigma*T^0.5))+(delT/3)*(f1(S0,u0,0)+4*f1(S0,u(1),delT)+2*f1(S0,u(2),2*delT))+summe1
end
As you can see, I have a number of functions ( Funk;Funkder and others ) which depend on values like K,T,M,S0 and so on that is entered by user in the editboxes. Here is for example code for Funk:
x(M)=(K*min(1,r/delta));
i=y1;
if i==M
summa1=0;
o=x(M)-K+0.5*(r*K-delta*x(M));
else
if (i+1)<(M-1)
for j=(i+1):1:(M-1)
fsum(j)=delT.*f1(x1(i),x1(j),(j-i)*delT);
end
summa1=sum(fsum);
o=x1(i)-K+p1(x1(i),K,(M-i)*delT)+0.5*delT*(f1(x1(i),x(M),(M-i)*delT)+0.5*(r*K-delta*x1(i)))+summa1;
else
summa1=0;
o=x1(i)-K+p1(x1(i),K,(M-i)*delT)+0.5*delT*(f1(x1(i),x(M),(M-i)*delT)+0.5*(r*K-delta*x1(i)))+summa1;
end
end
if~nargout;
o;
end
As you can see , Funk depends on the parameters that user enters from the keyboard. I have several following function that require those inputs. I am puzzled with first where I should have all those functions (like Funk ,Funkder and so on ) as well as how should send inputs to them . I have tried to have all such functions in the pushbutton callback before that piece of the code that responds for the calculations. In that case, I was getting no error message , but Matlab simply would get stuck . Then I tried to put those function outside pushbutton callback when then it seems like Matlab only can see the inputs from the editboxes just in pushbutton callback while I ,for example, get an error in the Funk that K is unknown variable because Matlab says "Undefined variable "handles" or class "handles.stock". I am very grateful that you are trying to help me and I hope I have explained my problem better this time. Best Regards

댓글 수: 2

Oleg Komarov
Oleg Komarov 2011년 4월 10일
I suggest to give a look at http://www.mathworks.com/matlabcentral/fileexchange/24861-41-complete-gui-examples
Dimitry
Dimitry 2011년 4월 10일
Thanks a lot !
Gonna check the link and hopefully it will solve my problem.
Regards

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

카테고리

도움말 센터File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

제품

질문:

2011년 4월 5일

Community Treasure Hunt

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

Start Hunting!

Translated by