Link two button group with push button in Matlab GUIDE

Hi,
I am trying to obtain a GUI with basically two button groups (with two buttons each) and a push button. First button group allows to chose between two functions and the same for the button group pannel. The user must select one function from the first button group and another from the second one, then when push button is selected the convolution of the two functions is provided. I would like to know how to load the selected functions from the button pannels in the pushbutton callback.
This is part of the code:
% --- Executes when selected object is changed in uipanel1. function uipanel1_SelectionChangeFcn(hObject, eventdata, handles) if hObject==handles.rect f=rect(x); axes(handles.axes1) stem(x,f) end
if hObject==handles.sinc
f=sinc(x);
guidata(hObject,handles)
axes(handles.axes1)
stem(x,f)
end
% --- Executes when selected object is changed in uipanel5. function uipanel5_SelectionChangeFcn(hObject, eventdata, handles)
if hObject==handles.cos g = cos(x);
axes(handles.axes2)
stem(x,g,'r')
end
if hObject==handles.tri
g=tri(x);
axes(handles.axes2)
stem(x,g,'r','filled')
end
% --- Executes on button press in convolucion.
function convolucion_Callback(hObject, eventdata, handles)
g = convn(f,g,'same')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Thank you in advance.

 Respuesta aceptada

Adam
Adam el 26 de Feb. de 2016
handles.uipanel5.SelectedObject
in your pushbutton callback will give you the selected object from that radiobutton panel (incidentally, you really should change the tags of all your GUI components to something sensible so you know easily what they refer to - e.g. 'panelTrigFunction' or whatever seems appropriate).
You can then compare this against the handles or the individual radio buttons in the panel (I tend to compare tags personally) and put in code as appropriate. Likewise you can access your other uibutton panel in the same way from the pushbutton callback.
In your example though it looks like what you are missing is something to the effect of:
handles.f = f;
guidata( hObject, handles )
in your first callback and the equivalent for g.
Then in your convolution callback you can use something like
res = convn(handles.f,handles.g,'same');
or
f = handles.f;
g = handles.g;
res = conv( f, g, 'same' )
If you really want you can reassign to 'g' here, but not sure that is what you would want.

3 comentarios

Hi Adam,
Thank you very much for your quick response.
I have tried to do as you suggested but I must be doing something wrong because I am not able to obtain the correct figure. I have rewritten my code for the first button group (renamed as first function) as follows:
% --- Executes when selected object is changed in firstfunction.
function firstfunction_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in firstfunction
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
handles.f=f;
guidata(hObject,handles)
handles.x=x;
guidata(hObject,handles)
handles.dx=dx;
guidata(hObject,handles)
if hObject==handles.rect
L=15;
M=200;
dx=L/M;
x=-L/2:dx:L/2-dx;
f = rect(x);
end
if hObject==handles.tri
L=15;
M=200;
dx=L/M;
x=-L/2:dx:L/2-dx;
f=tri(x);
end
I have done the same for the second button group (renamed as secondfunction) and within the pushbutton callback I have included the following:
% --- Executes on button press in convolucion.
function convolucion_Callback(hObject, eventdata, handles)
% hObject handle to convolucion (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.firstfunction.SelectedObject
handles.secondfuncion.SelectedObject
h = convn(handles.f,handles.g,'same')*dx;
stem(x,h)
When I run the gui and press the pushbutton (named as convolution) I obtain the following warning
"??? Error: File: Convolucion.m Line: 107 Column: 1 "f" previously appeared to be used as a function or command, conflicting with its use here as the name of a variable. A possible cause of this error is that you forgot to initialize the variable, or you have initialized it implicitly using load or eval."
Is it possible I am writing the commands that you indicated me in the wrong position?
Thank you for your time.
Ana
handles.f=f;
guidata(hObject,handles)
must go after you have defined 'f'. All that is doing is copying your local workspace 'f' into the handles struct of the GUI so it can be accessed in other callbacks too.
Where you have done it 'f' appears to be undefined because you then go on to define it afterwards. Apart from handle-derived classes Matlab objects use copy by value syntax, not copy by reference so handles.f will not change when f changes after the assignment to handles.
Thank you very much, Adam, it works nice now.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Preguntada:

el 26 de Feb. de 2016

Comentada:

el 29 de Feb. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by