Excecute computation on push button
2 views (last 30 days)
Show older comments
Hello, i have a problem with Push Button in matlab 2020b
The input was fine but when I assign the push button, the mathematic computation is not executed , please help
the program execute to record the edit 1 to 6 is fine, but the value of ts,fs,m,c,...etc are not computed when i push the button, what happen here?
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% Last Modified by GUIDE v2.5 09-Aug-2021 14:32:49
function edit1_Callback(hObject, eventdata, handles)
fc = str2double(get(handles.edit1,'String'));
assignin('base','fc',fc);
function edit2_Callback(hObject, eventdata, handles)
fm = str2double(get(handles.edit2,'String'));
assignin('base','fm',fm);
function edit3_Callback(hObject, eventdata, handles)
fd = str2double(get(handles.edit3,'String'));
assignin('base','fd',fd);
function edit4_Callback(hObject, eventdata, handles)
B = str2double(get(handles.edit4,'String'));
assignin('base','B',B);
function edit5_Callback(hObject, eventdata, handles)
Am = str2double(get(handles.edit5,'String'));
assignin('base','Am',Am);
function edit6_Callback(hObject, eventdata, handles)
Ac = str2double(get(handles.edit6,'String'));
assignin('base','Ac',Ac);
function pushbutton1_Callback(hObject, eventdata, handles)
global fc fm fd B Am Ac;
ts = 1/(10*fc);
fs= 1/ts;
t = 0:1/fs:0.5;
m=Am*cos(2*pi*fm*t); %message
c=Ac*cos(2*pi*fc*t); %carrier
y= fmmod(m,fc,fs,fd); %modulated
z= fmdemod(y,fc,fs,fd); %demodulated
3 Comments
Rik
on 9 Aug 2021
What is your evidence for that code not executing? Note that it is a function and has its own workspace. You will not (and should not) see anything happen in the base workspace.
Answers (2)
Jan
on 9 Aug 2021
With
assignin('base','Ac',Ac);
you create the variable Ac in the base workspace. If you do not declare this variable as global in the base workspace before, it is existing there, but not a global. Then:
function pushbutton1_Callback(hObject, eventdata, handles)
global fc fm fd B Am Ac
creates Ac as global variable, but this does not copy the value from the base workspace, when it wasn't a global there.
Global variables and assignin are really bad choices to distribute variables. Mixing them is even more confusing.
A clean usage of a GUI will store the variables internally, e.g. in the figure's UserData or in the handles struct.
function varargout = GUI(varargin)
% GUI MATLAB code for GUI.fig
% Last Modified by GUIDE v2.5 09-Aug-2021 14:32:49
function edit1_Callback(hObject, eventdata, handles)
handles.fc = str2double(get(handles.edit1,'String'));
guidata(hObject, handles);
function edit2_Callback(hObject, eventdata, handles)
handles.fm = str2double(get(handles.edit2,'String'));
guidata(hObject, handles);
... and so on
function pushbutton1_Callback(hObject, eventdata, handles)
ts = 1/(10*handles.fc);
fs= 1/handles.ts;
t = 0:1/handles.fs:0.5;
m=Am*cos(2*pi*handles.fm*handles.t); %message
c=Ac*cos(2*pi*handles.fc*handles.t); %carrier
y= fmmod(m,handles.fc,fs,handles.fd); %modulated
z= fmdemod(y,handles.fc,fs,handles.fd); %demodulated
end
Then you can run 2 GUIs in parallel ithout any confusions, because the valiues are store inside the figure.
An alternativ is to declare the variables as globals:
function edit1_Callback(hObject, eventdata, handles)
global fc
fc = str2double(get(handles.edit1,'String'));
end
function edit2_Callback(hObject, eventdata, handles)
global fm
fm = str2double(get(handles.edit2,'String'));
end
... and so on
function pushbutton1_Callback(hObject, eventdata, handles)
global fc fm fd B Am Ac
ts = 1/(10*fc);
fs= 1/ts;
t = 0:1/fs:0.5;
m=Am*cos(2*pi*fm*t); %message
c=Ac*cos(2*pi*fc*t); %carrier
y= fmmod(m,fc,fs,fd); %modulated
z= fmdemod(y,fc,fs,fd); %demodulated
end
Then there is no indirection over the base workspace anymore, but running 2 GUIs would not work, because they share the same values again.
Image Analyst
on 10 Aug 2021
@To Tran Luan, set a breakpoint and see if it gets inside the callback.
If it doesn't, check the Tag in GUIDE to make sure it has exactly the same name as the callback function, but with _Callback added to it. Because chances are, if it doesn't go in there, that the names are different.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!