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. 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. It worked, but the graph won't show when I run it with the GUI.
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. Also the str2double()
function was used to parse the values of the uieditfield
s.
%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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.