function functionGenerator % 创建主界面 mainFig = figure('Name', '函数发生器', 'NumberTitle', 'off', 'Position', [200, 200, 400, 300]);

% 创建函数选择模块
functionPanel = uipanel('Title', '函数选择', 'Position', [0.05, 0.5, 0.4, 0.45], 'Parent', mainFig);

% 创建函数选择按钮
sineButton = uicontrol('Style', 'radiobutton', 'String', '正弦函数', 'Position', [10, 80, 120, 20], 'Parent', functionPanel);
squareButton = uicontrol('Style', 'radiobutton', 'String', '方波函数', 'Position', [10, 50, 120, 20], 'Parent', functionPanel);
sawtoothButton = uicontrol('Style', 'radiobutton', 'String', '锯齿波函数', 'Position', [10, 20, 120, 20], 'Parent', functionPanel);

% 创建参数设置模块
paramPanel = uipanel('Title', '参数设置', 'Position', [0.5, 0.5, 0.4, 0.45], 'Parent', mainFig);

% 创建参数设置输入框
freqLabel = uicontrol('Style', 'text', 'String', '频率:', 'Position', [10, 80, 60, 20], 'Parent', paramPanel);
freqEdit = uicontrol('Style', 'edit', 'Position', [80, 80, 100, 20], 'Parent', paramPanel);
ampLabel = uicontrol('Style', 'text', 'String', '振幅:', 'Position', [10, 50, 60, 20], 'Parent', paramPanel);
ampEdit = uicontrol('Style', 'edit', 'Position', [80, 50, 100, 20], 'Parent', paramPanel);

% 创建函数显示模块
plotPanel = uipanel('Title', '函数波形', 'Position', [0.05, 0.05, 0.9, 0.4], 'Parent', mainFig);

% 创建显示图形的坐标系
axesHandle = axes('Parent', plotPanel, 'Position', [0.1, 0.1, 0.8, 0.8]);

% 创建绘图按钮
plotButton = uicontrol('Style', 'pushbutton', 'String', '绘图', 'Position', [170, 10, 60, 30], 'Parent', mainFig);
set(plotButton, 'Callback', {@plotButtonCallback, sineButton, squareButton, sawtoothButton, freqEdit, ampEdit, axesHandle});

end

% 绘图按钮的回调函数 function plotButtonCallback(hObject, ~, sineButton, squareButton, sawtoothButton, freqEdit, ampEdit, axesHandle) % 获取函数选择按钮的状态 sineSelected = get(sineButton, 'Value'); squareSelected = get(squareButton, 'Value'); sawtoothSelected = get(sawtoothButton, 'Value');

% 获取参数输入框的值
freq = str2double(get(freqEdit, 'String'));
amp = str2double(get(ampEdit, 'String'));

% 根据选择的函数类型生成相应的函数
if sineSelected
    x = linspace(0, 2*pi, 1000);
    y = amp * sin(freq * x);
    plot(axesHandle, x, y);
    xlabel(axesHandle, 'x');
    ylabel(axesHandle, 'y');
    title(axesHandle, '正弦函数');
elseif squareSelected
    x = linspace(0, 2*pi, 1000);
    y = amp * square(freq * x);
    plot(axesHandle, x, y);
    xlabel(axesHandle, 'x');
    ylabel(axesHandle, 'y');
    title(axesHandle, '方波函数');
elseif sawtoothSelected
    x = linspace(0, 2*pi, 1000);
    y = amp * sawtooth(freq * x);
    plot(axesHandle, x, y);
    xlabel(axesHandle, 'x');
    ylabel(axesHandle, 'y');
    title(axesHandle, '锯齿波函数');
end

end

% 调用函数发生器 functionGenerator();


原文地址: https://www.cveoy.top/t/topic/pq1r 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录