前些日子,被一朋友拜托了一课设,不是很难,但基于matlab GUI的设计中文论坛资源较少,所以我做完顺便分享一下。
程序主要内容:
效果展示:
主要代码:
代码展示,复制粘贴不能直接执行,请在code文件中下载,并在matlab界面中打开该code文件执行,才可正常运行。
classdef WaveCreater < matlab.apps.AppBase% Properties that correspond to app componentsproperties (Access = public)UIFigure matlab.ui.FigureSlider2 matlab.ui.control.SliderSlider_2 matlab.ui.control.SliderSlider matlab.ui.control.SliderwitdthN matlab.ui.control.NumericEditFieldLabel_7 matlab.ui.control.LabeldelayN matlab.ui.control.NumericEditFieldLabel_6 matlab.ui.control.LabelButton_3 matlab.ui.control.ButtonCheckBox matlab.ui.control.CheckBoxButton_2 matlab.ui.control.ButtonDropDown_2 matlab.ui.control.DropDownLabel_5 matlab.ui.control.LabelEroP matlab.ui.control.NumericEditFieldLabel_4 matlab.ui.control.LabeltimessEditField matlab.ui.control.NumericEditFieldtimessEditFieldLabel matlab.ui.control.LabelsEditField matlab.ui.control.EditFieldsEditFieldLabel matlab.ui.control.Labelintensi matlab.ui.control.NumericEditFieldLabel_3 matlab.ui.control.LabelKHzEditField matlab.ui.control.NumericEditFieldLabel_2 matlab.ui.control.LabelDropDown matlab.ui.control.DropDownLabel matlab.ui.control.LabelButton matlab.ui.control.ButtonUIAxes matlab.ui.control.UIAxesendproperties (Access = private)Check=false; % Descriptionendmethods (Access = private)function x = CreatW(app)% 参数设置Fs = app.timessEditField.Value; % 采样率(每秒采样点数)T = 1/Fs; % 采样周期L = str2double(app.sEditField.Value)*Fs; % 信号长度t = (0:L-1)*T; % 时间向量N = length(t); % 假设 t 是时间向量f =app.KHzEditField.Value/1000; % 信号频率(Hz/μs)A = app.intensi.Value; % 信号幅度ep=app.EroP.Value; %噪声值x = zeros(1, N); % 预分配信号数组Wid=app.witdthN.Value;%锯齿波宽度dT=(1/f)*app.delayN.Value/(2*pi);if app.DropDown.ValueIndex==1 % 正弦波参数x = A * (sin(2*pi*f*(t+dT)) + ep/100*rand(1,L));elseif app.DropDown.ValueIndex==2 %方波x = A* (square(2*pi*(t+dT)*f, 50)+ep/100*rand(1,L));elseif app.DropDown.ValueIndex==3 %锯齿波x = A*(sawtooth(f*pi*(t+dT)*2, Wid)+rand(1,L)*ep/100);end% 绘制信号plot(app.UIAxes,t, x);app.Button_2.Enable="on";app.Button_3.Enable='on';endend% Callbacks that handle component eventsmethods (Access = private)% Value changed function: CheckBoxfunction CheckBoxValueChanged(app, event)if app.CheckBox.Value==1app.Check=true;app.Button.Enable="off";elseapp.Button.Enable='on';endif app.Checkapp.CreatW;endend% Button pushed function: Buttonfunction ButtonPushed(app, event)app.CreatW;end% Value changed function: DropDownfunction DropDownValueChanged(app, event)if app.Checkapp.CreatW;endif app.DropDown.ValueIndex==3app.witdthN.Enable='on';endend% Value changed function: KHzEditFieldfunction KHzEditFieldValueChanged(app, event)if app.Checkapp.CreatW;endif app.Slider.Value~=app.KHzEditField.Valueapp.Slider.Value=app.KHzEditField.Value;endend% Value changed function: intensifunction intensiValueChanged(app, event)if app.Checkapp.CreatW;endif app.Slider_2.Value~=app.intensi.Valueapp.Slider_2.Value=app.intensi.Value;endend% Value changed function: sEditFieldfunction sEditFieldValueChanged(app, event)if app.Checkapp.CreatW;endend% Value changed function: timessEditFieldfunction timessEditFieldValueChanged(app, event)if app.Checkapp.CreatW;endend% Value changed function: EroPfunction EroPValueChanged(app, event)if app.Checkapp.CreatW;endend% Button pushed function: Button_2function Button_2Pushed(app, event)% 参数设置Fs = app.timessEditField.Value; % 采样率(每秒采样点数)T = 1/Fs; % 采样周期L = str2double(app.sEditField.Value)*Fs; % 信号长度t = (0:L-1)*T; % 时间向量N = length(t); % 假设 t 是时间向量f =app.KHzEditField.Value/1000; % 信号频率(Hz/μs)A = app.intensi.Value; % 信号幅度ep=app.EroP.Value; %噪声值x = zeros(1, N); % 预分配信号数组Wid=app.witdthN.Value;%锯齿波宽度dT=(1/f)*app.delayN.Value/(2*pi);if app.DropDown.ValueIndex==1 % 正弦波参数x = A * (sin(2*pi*f*(t+dT)) + ep/100*rand(1,L));elseif app.DropDown.ValueIndex==2 %方波x = A* (square(2*pi*(t+dT)*f, 50)+ep/100*rand(1,L));elseif app.DropDown.ValueIndex==3 %锯齿波x = A*(sawtooth(f*pi*(t+dT)*2, Wid)+rand(1,L)*ep/100);end% 绘制信号figureplot(t, x);end% Value changed function: witdthNfunction witdthNValueChanged(app, event)if app.Checkapp.CreatW;endend% Value changed function: delayNfunction delayNValueChanged(app, event)if app.Checkapp.CreatW;endif app.Slider2.Value~=app.delayN.Valueapp.Slider2.Value=app.delayN.Value;endend% Button pushed function: Button_3function Button_3Pushed(app, event)% 获取用户选择的文件名x = CreatW(app);[filename, pathname] = uiputfile({'*.mat', 'MATLAB Files (*.mat)';...'*.*', 'All Files (*.*)'}, ...'Save Parameters');% 如果用户选择了文件名if ~isequal(filename,0)% 拼接完整的文件路径fullPath = fullfile(pathname, filename);% 这里可以保存你的参数或数据到选择的文件中% 例如,保存一个变量 myData 到 MAT 文件save(fullPath, 'x');endend% Value changed function: Sliderfunction SliderValueChanged(app, event)app.KHzEditField.Value = app.Slider.Value;if app.Checkapp.CreatW;endend% Value changed function: Slider_2function Slider_2ValueChanged(app, event)app.intensi.Value = app.Slider_2.Value;if app.Checkapp.CreatW;endend% Value changed function: Slider2function Slider2ValueChanged(app, event)app.delayN.Value = app.Slider2.Value;if app.Checkapp.CreatW;endendend% Component initializationmethods (Access = private)% Create UIFigure and componentsfunction createComponents(app)% Create UIFigure and hide until all components are createdapp.UIFigure = uifigure('Visible', 'off');app.UIFigure.Position = [100 100 755 568];app.UIFigure.Name = 'MATLAB App';% Create UIAxesapp.UIAxes = uiaxes(app.UIFigure);title(app.UIAxes, '信号波形')xlabel(app.UIAxes, '时间(μs)')ylabel(app.UIAxes, '信号强度')app.UIAxes.Position = [14 19 458 505];% Create Buttonapp.Button = uibutton(app.UIFigure, 'push');app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);app.Button.Position = [524 80 100 23];app.Button.Text = '生成信号';% Create Labelapp.Label = uilabel(app.UIFigure);app.Label.HorizontalAlignment = 'right';app.Label.Position = [546 533 53 22];app.Label.Text = '信号类型';% Create DropDownapp.DropDown = uidropdown(app.UIFigure);app.DropDown.Items = {'正弦波', '方波', '锯齿波'};app.DropDown.ValueChangedFcn = createCallbackFcn(app, @DropDownValueChanged, true);app.DropDown.Position = [614 533 100 22];app.DropDown.Value = '正弦波';% Create Label_2app.Label_2 = uilabel(app.UIFigure);app.Label_2.HorizontalAlignment = 'right';app.Label_2.Position = [516 492 84 22];app.Label_2.Text = '信号频率(KHz)';% Create KHzEditFieldapp.KHzEditField = uieditfield(app.UIFigure, 'numeric');app.KHzEditField.Limits = [100 200];app.KHzEditField.ValueChangedFcn = createCallbackFcn(app, @KHzEditFieldValueChanged, true);app.KHzEditField.HorizontalAlignment = 'center';app.KHzEditField.Position = [615 492 100 22];app.KHzEditField.Value = 100;% Create Label_3app.Label_3 = uilabel(app.UIFigure);app.Label_3.HorizontalAlignment = 'right';app.Label_3.Position = [555 403 53 22];app.Label_3.Text = '信号幅度';% Create intensiapp.intensi = uieditfield(app.UIFigure, 'numeric');app.intensi.Limits = [0 10];app.intensi.ValueChangedFcn = createCallbackFcn(app, @intensiValueChanged, true);app.intensi.HorizontalAlignment = 'center';app.intensi.Position = [623 403 100 22];app.intensi.Value = 1;% Create sEditFieldLabelapp.sEditFieldLabel = uilabel(app.UIFigure);app.sEditFieldLabel.HorizontalAlignment = 'right';app.sEditFieldLabel.Position = [508 226 90 22];app.sEditFieldLabel.Text = '显示范围(μs)';% Create sEditFieldapp.sEditField = uieditfield(app.UIFigure, 'text');app.sEditField.InputType = 'digits';app.sEditField.ValueChangedFcn = createCallbackFcn(app, @sEditFieldValueChanged, true);app.sEditField.HorizontalAlignment = 'center';app.sEditField.Position = [613 226 100 22];app.sEditField.Value = '20';% Create timessEditFieldLabelapp.timessEditFieldLabel = uilabel(app.UIFigure);app.timessEditFieldLabel.HorizontalAlignment = 'right';app.timessEditFieldLabel.Position = [489 185 110 22];app.timessEditFieldLabel.Text = '采样率(times/μs)';% Create timessEditFieldapp.timessEditField = uieditfield(app.UIFigure, 'numeric');app.timessEditField.Limits = [1 100];app.timessEditField.ValueChangedFcn = createCallbackFcn(app, @timessEditFieldValueChanged, true);app.timessEditField.HorizontalAlignment = 'center';app.timessEditField.Position = [614 185 100 22];app.timessEditField.Value = 5;% Create Label_4app.Label_4 = uilabel(app.UIFigure);app.Label_4.HorizontalAlignment = 'right';app.Label_4.Position = [486 112 112 22];app.Label_4.Text = '噪声相对强度(%)';% Create EroPapp.EroP = uieditfield(app.UIFigure, 'numeric');app.EroP.Limits = [0 100];app.EroP.ValueChangedFcn = createCallbackFcn(app, @EroPValueChanged, true);app.EroP.HorizontalAlignment = 'center';app.EroP.Position = [613 112 100 22];app.EroP.Value = 5;% Create Label_5app.Label_5 = uilabel(app.UIFigure);app.Label_5.HorizontalAlignment = 'right';app.Label_5.Position = [546 150 53 22];app.Label_5.Text = '噪声类型';% Create DropDown_2app.DropDown_2 = uidropdown(app.UIFigure);app.DropDown_2.Items = {'高斯'};app.DropDown_2.Position = [614 150 100 22];app.DropDown_2.Value = '高斯';% Create Button_2app.Button_2 = uibutton(app.UIFigure, 'push');app.Button_2.ButtonPushedFcn = createCallbackFcn(app, @Button_2Pushed, true);app.Button_2.Enable = 'off';app.Button_2.Position = [523 41 185 32];app.Button_2.Text = '自定义波形图像';% Create CheckBoxapp.CheckBox = uicheckbox(app.UIFigure);app.CheckBox.ValueChangedFcn = createCallbackFcn(app, @CheckBoxValueChanged, true);app.CheckBox.Text = '实时生成';app.CheckBox.Position = [646 81 70 22];% Create Button_3app.Button_3 = uibutton(app.UIFigure, 'push');app.Button_3.ButtonPushedFcn = createCallbackFcn(app, @Button_3Pushed, true);app.Button_3.Enable = 'off';app.Button_3.Position = [524 10 190 23];app.Button_3.Text = '保存波形数据';% Create Label_6app.Label_6 = uilabel(app.UIFigure);app.Label_6.HorizontalAlignment = 'right';app.Label_6.Position = [539 321 65 22];app.Label_6.Text = '相位偏移角';% Create delayNapp.delayN = uieditfield(app.UIFigure, 'numeric');app.delayN.Limits = [0 6.28318530717959];app.delayN.ValueChangedFcn = createCallbackFcn(app, @delayNValueChanged, true);app.delayN.HorizontalAlignment = 'center';app.delayN.Position = [619 321 100 22];% Create Label_7app.Label_7 = uilabel(app.UIFigure);app.Label_7.HorizontalAlignment = 'right';app.Label_7.Enable = 'off';app.Label_7.Position = [63 533 283 22];app.Label_7.Text = '锯齿波上升比(为0是标准锯齿波,为0.5是三角波)';% Create witdthNapp.witdthN = uieditfield(app.UIFigure, 'numeric');app.witdthN.ValueChangedFcn = createCallbackFcn(app, @witdthNValueChanged, true);app.witdthN.Enable = 'off';app.witdthN.Position = [361 533 100 22];% Create Sliderapp.Slider = uislider(app.UIFigure);app.Slider.Limits = [100 200];app.Slider.ValueChangedFcn = createCallbackFcn(app, @SliderValueChanged, true);app.Slider.Position = [564 469 150 3];app.Slider.Value = 100;% Create Slider_2app.Slider_2 = uislider(app.UIFigure);app.Slider_2.Limits = [0 10];app.Slider_2.ValueChangedFcn = createCallbackFcn(app, @Slider_2ValueChanged, true);app.Slider_2.Position = [565 382 150 3];% Create Slider2app.Slider2 = uislider(app.UIFigure);app.Slider2.Limits = [0 6.28318530717959];app.Slider2.ValueChangedFcn = createCallbackFcn(app, @Slider2ValueChanged, true);app.Slider2.Position = [568 294 150 3];% Show the figure after all components are createdapp.UIFigure.Visible = 'on';endend% App creation and deletionmethods (Access = public)% Construct appfunction app = WaveCreater% Create UIFigure and componentscreateComponents(app)% Register the app with App DesignerregisterApp(app, app.UIFigure)if nargout == 0clear appendend% Code that executes before app deletionfunction delete(app)% Delete UIFigure when app is deleteddelete(app.UIFigure)endend
end
Code文件下载:
网盘链接: https://pan.baidu.com/s/1e3WKdpiMstkRgEr1AlJdzw
提取码: bq9u