1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
| function programmatic_gui fig = uifigure('Name', '数据绘图工具', ... 'Position', [100, 100, 600, 400]); ax = uiaxes('Parent', fig, ... 'Position', [200, 50, 350, 300]); dropdown = uidropdown('Parent', fig, ... 'Position', [50, 300, 100, 22], ... 'Items', {'正弦函数', '余弦函数', '随机数据'}); btn = uibutton('Parent', fig, ... 'Position', [50, 250, 100, 30], ... 'Text', '绘图', ... 'ButtonPushedFcn', @plot_data); function plot_data(~, ~) x = linspace(0, 2*pi, 100); selected = dropdown.Value; cla(ax); switch selected case '正弦函数' y = sin(x); plot(ax, x, y, 'b-', 'LineWidth', 2); title(ax, '正弦函数'); case '余弦函数' y = cos(x); plot(ax, x, y, 'r-', 'LineWidth', 2); title(ax, '余弦函数'); case '随机数据' y = randn(1, 100); scatter(ax, 1:100, y, 'filled'); title(ax, '随机数据'); end grid(ax, 'on'); end end
|