필터 지우기
필터 지우기

Excecute computation on push button

조회 수: 4 (최근 30일)
To Tran Luan
To Tran Luan 2021년 8월 9일
답변: Image Analyst 2021년 8월 10일
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
To Tran Luan
To Tran Luan 2021년 8월 9일
thank you, i need help with the pushbutton, it seem something wrong because when i push the button , it doesnt execute the
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
Rik
Rik 2021년 8월 9일
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.

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

답변 (2개)

Jan
Jan 2021년 8월 9일
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.
  댓글 수: 1
To Tran Luan
To Tran Luan 2021년 8월 10일
thank you its works, very effectively

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


Image Analyst
Image Analyst 2021년 8월 10일
@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.

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by