필터 지우기
필터 지우기

pass handles to function nested in callback

조회 수: 1 (최근 30일)
Thomas Casey
Thomas Casey 2014년 10월 26일
댓글: Geoff Hayes 2014년 10월 26일
I have a function nested in a button_callback that I call from within the same callback with fminsearch when the button is pressed. As far as I know, the function called by fminsearch has to get only one input, the input corresponding to the parameter that is varied in the optimization. Is that true? How can I pass my handles structure to the function called by fminsearch without making everything global? Below is an example of the code, the error I get is that "handles.whatever is non-existent" in the nested function.
function pushbutton7_Callback(hObject, eventdata, handles)
guidata(hObject,handles)
options=optimset('Tolfun',1e-15,'Display','off');
handles.fitvalinp=fminsearch(@LSE_synthetas,handles.disinp,options);
function chisq=LSE_synthetas(vary_param)
%%synthetic gaussians
fe1=zeros(length(handles.dist),length(handles.valinp));
chisq=0;
for i=1:length(handles.valinp)
fe1(:,i)=vary_param(i)*gaussian(handles.dist,handles.disinp(i),handles.stdev(i));
for j=1:length(handles.dist)
chisq=chisq+((fe1(j,i)-handles.rel_pop(j,1)).^2);
end
end
axes(handles.axes3)
plot(handles.dist,fe1,'-',handles.dist,handles.rel_pop,'--')
drawnow
return
  댓글 수: 1
Geoff Hayes
Geoff Hayes 2014년 10월 26일
Thomas - the fact that the error message Reference to non-existent field XYZ appears suggests that the handles structure is accessible to the nested function LSE_synthesas. So which field does the error correspond to, and where is this field initialized? It may be that once it has been initialized you haven't called the following
guidata(hObject,handles)
which will save the changes to the updated handles object.
I noticed that you have the above line as the first line of code in your function. Since there have been no changes to handles prior to that call, then it is not necessary (and so can be removed).

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by