我可以在 MATLAB App Designer 中包含茎图吗

问题描述 投票:0回答:1

我正在尝试在 Matlab App Designer 中创建带有茎图的随机信号生成器,当我单击“生成”按钮时,什么也没有发生。以下是我在应用程序设计器生成的代码中添加的代码。

methods (Access = private)
    
    %generate button is pushed
    function GenerateButtonPushed(app, event)
        amplitude = app.AmplitudeEditField.Value;
        samples = app.SamplesEditField.Value;
        n_range1 = app.nRange1EditField.Value;
        n_range2 = app.nRange2EditField.Value;
        n_range = n_range1:n_range2;
        
        xn = amplitude .* sin(2*pi*randn(1,samples));
        
        %plot random signal
        stem(n_range,xn, 'parent', app.UIAxes)
        
    end
end

当我在 Matlab 的实时脚本中运行这段代码时。它有效,但当我使用 GUI 运行它时,图表不会显示。

matlab plot signal-processing
1个回答
0
投票

以编程方式创建随机信号发生器

很抱歉,如果这与您当前所拥有的有很大偏差,但这里是一个信号生成器的示例,它将各个字段作为输入传递到回调函数中。

str2double()
函数还用于解析
uieditfield
的值。

%App figure properties%
App = uifigure();
App.Name = "Random Signal Generator";
X_Position = 200;
Y_Position = 220;
Height = 300;
Width = 650;
App.Position = [X_Position Y_Position Width Height];

%Amplitude field%
X_Position = 150;
Y_Position = 245;
Height = 30; Width = 90;
AmplitudeEditField = uieditfield('Parent',App);
AmplitudeEditField.Position = [X_Position Y_Position Width Height];

%Number of samples field%
SamplesEditField = uieditfield('Parent',App);
SamplesEditField.Position = [X_Position Y_Position-40 Width Height];

%Start range field%
nRange1EditField = uieditfield('Parent',App);
nRange1EditField.Position = [X_Position Y_Position-80 Width Height];

%End range field%
nRange2EditField = uieditfield('Parent',App);
nRange2EditField.Position = [X_Position Y_Position-120 Width Height];

Label_1 = uilabel('Parent',App);
Label_1.Position = [X_Position-90 Y_Position Width Height];
Label_1.Text = "Amplitude";

Label_2 = uilabel('Parent',App);
Label_2.Position = [X_Position-90 Y_Position-40 Width Height];
Label_2.Text = "Number of Samples";

Label_3 = uilabel('Parent',App);
Label_3.Position = [X_Position-90 Y_Position-80 Width Height];
Label_3.Text = "Start of Range";

Label_4 = uilabel('Parent',App);
Label_4.Position = [X_Position-90 Y_Position-120 Width Height];
Label_4.Text = "End of Range";

Label_Set = findall(App,'Type','uilabel');
Red = 0.2; Green = 0.2; Blue = 0.2;
set(Label_Set,'BackgroundColor',[Red Green Blue]);
set(Label_Set,'FontColor','white');
set(Label_Set,'FontSize',9);
set(Label_Set,'HorizontalAlignment','center');

%Calculate button properties%
Generate_Button = uibutton('Parent',App);
X_Position = 60; Y_Position = 60;
Height = 40; Width = 180;
Generate_Button.Position = [X_Position Y_Position Width Height];
Generate_Button.Text = "Generate";
Red = 0.7; Green = 0.7; Blue = 0.7;
Generate_Button.BackgroundColor = [Red Green Blue];


UIAxes = uiaxes(App);
X_Position = 300;
Y_Position = 50;
Height = 220;
Width = 300;
UIAxes.Position = [X_Position Y_Position Width Height];
UIAxes.XLabel.String = 'Sample Index';
UIAxes.YLabel.String = 'Amplitude';
UIAxes.Title.String = 'Generated Signal';


Generate_Button.ButtonPushedFcn = @(Generate_Button,event) GenerateButtonPushed(AmplitudeEditField,SamplesEditField,nRange1EditField,nRange2EditField,UIAxes);


function GenerateButtonPushed(AmplitudeEditField,SamplesEditField,nRange1EditField,nRange2EditField,UIAxes)

    amplitude = (AmplitudeEditField.Value);
    samples = (SamplesEditField.Value);
    n_range1 = (nRange1EditField.Value);
    n_range2 = (nRange2EditField.Value);
 
    if(amplitude ~= "" && samples ~= "" && n_range1 ~= "" && n_range2~= "")
    
    amplitude = str2double(amplitude);
    samples = str2double(samples);
    n_range1 = str2double(n_range1);
    n_range2 = str2double(n_range2);
    n_range = linspace(n_range1,n_range2,samples);
    xn = amplitude .* sin(2*pi*randn(1,samples));
    stem(UIAxes,n_range,xn);

    end

end
© www.soinside.com 2019 - 2024. All rights reserved.