简体   繁体   English

我可以在 MATLAB App Designer 中包含一个词干 plot

[英]Can I include a stem plot in MATLAB App Designer

I'm trying to create a random signal generator with stem plot in Matlab App Designer, and when I click the 'Generate' Button - nothing happens.我正在尝试在 Matlab 应用程序设计器中创建一个带有茎 plot 的随机信号发生器,当我单击“生成”按钮时 - 没有任何反应。 Below is the code I added in the generated code from the 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

When I ran this block of code in a live script in Matlab.当我在 Matlab 的实时脚本中运行这段代码时。 It worked, but the graph won't show when I run it with the GUI.它有效,但是当我使用 GUI 运行它时,图表不会显示。

Programatically Created Random Signal Generator以编程方式创建的随机信号发生器

Sorry if this deviates significantly from what you currently have, but here is an example of a signal generator that passes the individual fields as inputs into the callback function.抱歉,如果这与您当前所拥有的有很大不同,但这里是一个信号生成器的示例,它将各个字段作为输入传递给回调 function。 Also the str2double() function was used to parse the values of the uieditfield s. str2double() function 还用于解析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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM