matlab GUI学习

1.命令输入guide
2.建立GUI
3.添加控件
  • Static Text 静态文本框
  • Edit Text 编辑文本框
  • Pushbutton 按钮
  • Radiobutton 单选
  • Axes 坐标轴
4.双击设置控件参数并保存

在这里插入图片描述

5.书写GUI回调函数(callback)代码
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(gcf);
run('sigsample');

单选:

% --- Executes on button press in radiobutton1.
function radiobutton1_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton1
set(handles.radiobutton1,'value',1);
set(handles.radiobutton2,'value',0);

% --- Executes on button press in radiobutton2.
function radiobutton2_Callback(hObject, eventdata, handles)
% hObject    handle to radiobutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of radiobutton2
set(handles.radiobutton2,'value',1);
set(handles.radiobutton1,'value',0);

打开音频文件:

% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton5 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

    [filename, pathname] = uigetfile('*.wav', '*.*','请选择一个音频文件');
    
    if isequal(filename,0) | isequal(pathname,0)
       return;
    end
    
    newstr=[pathname filename];
    set(handles.edit6,'String',newstr);
    
    [x,fs,nbits]=wavread([pathname filename]);
    set(handles.edit9,'String',num2str(fs));
    
    axes(handles.axes1);
    plot(x);
    xlabel('时间');
    ylabel('幅度');
    
    N=length(x);
    fx=fftshift(fft(x));
    w=linspace(-fs/2,fs/2,N);
    axes(handles.axes2);
    plot(w,abs(fx));
    xlabel('f');
    ylabel('幅度');
    
    handles.x=x;
    handles.fs=fs;
    % Choose default command line output for sigsample
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

set(handles.pushbutton7,'enable','on');
set(handles.pushbutton8,'enable','on');

频率转换:

% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton7 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
%频率转换
x=handles.x;
fs=handles.fs;
fsn=str2double(get(handles.edit7,'string'));
t1=(fs/fsn:fs/fsn:length(x))/fs;     
c=resample(x,fsn,fs) ;   
wavwrite(c,fsn,'after.wav')      
axes(handles.axes1);  
plot(t1,c); 
xlabel('时间');
ylabel('幅度');
    N=length(c);
    fx=fftshift(fft(c));
    w=linspace(-fsn/2,fsn/2,N);
    axes(handles.axes2);
    plot(w,abs(fx));
    xlabel('f');
    ylabel('幅度');

播放:

% --- Executes on button press in pushbutton8.
function pushbutton8_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton8 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

x=handles.x;
fs=handles.fs;
sound(x,fs);

录音:

% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton6 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%录音
fs=str2double(get(handles.edit17,'string'));           %取样频率
duration=str2double(get(handles.edit18,'string'));         %录音时间
% fprintf('Press any key to start %g seconds of recording...\n',duration);
% pause;
fprintf('Recording...\n');
x=wavrecord(duration*fs,fs);         %duration*fs 是总的采样点数
msgbox('录制已经完成','录音','edit19');
wavwrite(x,fs,'recording.wav');%写入音频文件.wav

返回菜单界面:

% --- Executes on button press in pushbutton10.
function pushbutton10_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton10 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
close(gcf);
menu;
6.运行
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值