pass handles to function nested in callback

1 view (last 30 days)
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 Comment
Geoff Hayes
Geoff Hayes on 26 Oct 2014
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).

Sign in to comment.

Answers (0)

Categories

Find more on Interactive Control and Callbacks 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!