虽然打开matlab后提示说GUI后续版本会取消(以后的版本中将会删除 GUIDE。请改用 APPDESIGNER。 ),但是老师布置了matlab GUI实验,所以说还得学习学习,所以记录学习过程,学习笔记。
只需要将下面代码写在回调函数,就可以实现
s=get(hObject,"string");
set(hObject,"string",num2str(str2num(s)+1));
clear all;
close all;
s=get(hObject,"string");
set(handles.text2,"string",s);
%%0123456789+-/的回调函数
s0=get(handles.text2,'string');
s=get(hObject,'string');
set(handles.text2,'string',[s0,s]);
%%乘号的回调函数
s0=get(handles.text2,'string');
set(handles.text2,'string',[s0,'*']);
%%清屏的回调函数
set(handles.text2,'string',[]);
%%等于号的回调函数
s0=get(handles.text2,'string');
set(handles.text2,'string',num2str(eval(s0)));
%%按钮回调函数
xingming=get(handles.edit1,"string");
xingbie=get(handles.popupmenu1,"string");
v1=get(handles.popupmenu1,"value");
suoxuanxingbie=xingbie{v1};
banji=get(handles.popupmenu2,"string");
v2=get(handles.popupmenu2,"value");
suoxuanbanji=banji{v2};
c={xingming,suoxuanxingbie,suoxuanbanji};
set(handles.listbox1,"string",c);
%%列表框回调函数
xinxi=get(hObject,"string");
v=get(hObject,"value");
if v==2
if xinxi{2}=='男'
xinxi{2}='女';
else
xinxi{2}='男';
end
end
if v==3
if xinxi{3}=='一班'
xinxi{3}='二班';
elseif xinxi{3}=='二班'
xinxi{3}='三班';
else
xinxi{3}='一班';
end
end
set(hObject,"string",xinxi);
%%按钮的回调函数
v=get(handles.popupmenu1,"value");
fplot(handles.axes1,@sin,[0,2*pi*v]);
title('sin');
grid on;
%%edit1_CreateFcn 编辑框创造时的配置
set(hObject,"string",datestr(now,31));%%获得当前时间的值设置给编辑框的string
%%pushbutton2_Callback 按键的回调函数
shedingshijian=datenum(get(handles.edit1,"string"));%获得编辑框设定的值
while 1
dangqianshijian=datenum(now);
if shedingshijian==dangqianshijian
break;
end
end
helpdlg("时间到了","提示");
v=get(handles.checkbox1,"value");%是否播放音乐
if v==1
yinyue=get(handles.pushbutton1,"userdata");
dos(yinyue);
end
%%复选框的回调函数
v=get(hObject,"value");
if v==1
set(handles.pushbutton1,"enable","on");
else
set(handles.pushbutton1,"enable","off");
end
%%选择音乐按钮的回调函数
[wenjianmingcheng,lujing]=uigetfile({"*.mp3";"*.wav"},"选择音乐");
set(hObject,"string",wenjianmingcheng);
set(hObject,"userdata",fullfile(lujing,wenjianmingcheng));
%%打开按钮的回调函数
[~,~,data]=xlsread("fx36_7");
data(1,:)=[];
set(handles.uitable1,"data",data);
%%表格的编辑回调
ind=eventdata.Indices;
pr=eventdata.PreviousData;
ed=eventdata.EditData;
s=['第',num2str(ind(1)),'行第',num2str(ind(2)),'列的数据',num2str(pr),'更改为:',num2str(ed)];
set(handles.text2,"string",s);
%%保存按钮的回调函数
data=get(handles.uitable1,"data");
xlswrite("fx36_7_out.xlsx",data);
helpdlg("保存成功郭大大","提示");
% --- 选择音乐的按钮
function pushbutton1_Callback(hObject, eventdata, handles)
[wenjianmingcheng,lujing]=uigetfile("*.mp3","选择音乐");
global y; %定义全局变量
global Fs; %定义全局变量
if isequal(wenjianmingcheng,0)||isequal(lujing,0)
errordlg("没有选择语音信号","错误");
return;
else
im_1=strcat(lujing,wenjianmingcheng);
[y,Fs] = audioread(im_1);
y=y(:,1);
msgbox("语音信号读取成功","提示","help");
set(handles.edit1,"string",im_1);
end
% --- 画时域波形的按钮
function pushbutton2_Callback(hObject, eventdata, handles)
%%按钮的回调函数
global y; %全局变量使用前再申明
axes(handles.axes1); %%选定坐标轴
plot(handles.axes1,y); %画出y波形
xlabel("时间");ylabel("幅值");title('原信号时域波形');
% --- 播放按钮
function pushbutton3_Callback(hObject, eventdata, handles)
global y;
global Fs;
sound(y,Fs);
% --- 画频谱图的按钮
function pushbutton4_Callback(hObject, eventdata, handles)
global y;
global Fs;
N=length(y);
yy=fftshift(fft(y));
mag=abs(yy);
f=(floor(-(N-1)/2):floor((N-1)/2))/(N-1)*Fs;
axes(handles.axes2);
plot(handles.axes2,f,mag);grid;
xlabel("频率/Hz");ylabel("幅值");title('原信号的幅频图');
%%初始化的回调函数
comlist=seriallist("all");%找到活动的端口列表
app.serialport.Items=comlist;
%%创建一个属性scom
%%按钮回调函数
value = app.Switch.Value;
if value=="On"
app.STAUSLamp.Color="0.00,1.00,0.00";
app.scom=serial(app.serialport.Value);
app.scom.BaudRate=str2num(app.serialbaud.Value);
app.scom.BytesAvailableFcnMode="byte";
app.scom.BytesAvailableFcnCount=2;
set(app.scom,"BytesAvailableFcn",{@app.receive});
fopen(app.scom);
end
if value=="Off"
app.STAUSLamp.Color="0.00,0.00,0.00";
end
%%关闭回调函数
scoms=instrfind;
stopasync(scoms);
fclose(scoms);
delete(scoms);
delete(app)
%%增加一个函数 receive函数
%%创建一个属性rubffer
function results = receive(app,src,event)
if app.scom.BytesAvailable>0
temp=fread(app.scom,1,"uint16");
app.rubffer=[app.rubffer temp];
plot(app.ax1,app.buffer,"r");
end
end