Plotting on the axes with freqs() not working
Afficher commentaires plus anciens
Hello,so I'm basically working on a GUI with App Designer and the buttons work,but when I hit the Calculate button it doesn't plot on the FreqAxes.I have to use the freqs() function.
Here's the full code,I have no idea where I made a mistake,I'm beginner in Matlab and I learn it in University. (this is an univeristy project)
classdef matlab_app < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
KEdit matlab.ui.control.NumericEditField
KEditEditFieldLabel matlab.ui.control.Label
PEdit matlab.ui.control.NumericEditField
PEditEditFieldLabel matlab.ui.control.Label
APEdit matlab.ui.control.NumericEditField
APEditEditFieldLabel matlab.ui.control.Label
Calculate matlab.ui.control.Button
BEdit matlab.ui.control.NumericEditField
BEditLabel matlab.ui.control.Label
ZPEdit matlab.ui.control.NumericEditField
ZPEditLabel matlab.ui.control.Label
FilterTypeGroup matlab.ui.container.ButtonGroup
StopRadio matlab.ui.control.RadioButton
HighRadio matlab.ui.control.RadioButton
BandpassRadio matlab.ui.control.RadioButton
LowRadio matlab.ui.control.RadioButton
WoEditField matlab.ui.control.NumericEditField
WoEditFieldLabel matlab.ui.control.Label
OrderSpinner matlab.ui.control.Spinner
OrderSpinnerLabel matlab.ui.control.Label
SyntaxListBox matlab.ui.control.ListBox
FreqAxes matlab.ui.control.UIAxes
end
properties (Access = private)
b
a
z
p
k
n double
Wo
filterType
% Description
end
methods (Access = private)
% Setting the right ftype
function filterType = getFilterType(app)
if app.LowRadio.Value
filterType = 'low';
elseif app.BandpassRadio.Value
filterType = 'bandpass';
elseif app.HighRadio.Value
filterType = 'high';
elseif app.StopRadio.Value
filterType = 'stop';
else
filterType = '';
end
end
% Buttons reset
function resetControls(app)
app.ZPEdit.Enable = 'off';
app.APEdit.Enable = 'off';
app.BEdit.Enable = 'off';
app.PEdit.Enable = 'off';
app.KEdit.Enable = 'off';
end
%{
function plotFrequencyResponse(freq_response)
figure;
semilogx(freq_response(:,1), 20*log10(abs(freq_response(:,2))));
title('Frequency Response');
xlabel('Frequency (Hz)');
ylabel('Magnitude (dB)');
grid on;
end
%}
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: SyntaxListBox
function SyntaxListBoxValueChanged(app, event)
value = app.SyntaxListBox.Value;
app.resetControls();
switch value
case 'besself (b, a)'
app.APEdit.Enable = 'on';
app.BEdit.Enable = 'on';
case 'besself (z, p, k)'
app.ZPEdit.Enable = 'on';
app.PEdit.Enable = 'on';
app.KEdit.Enable = 'on';
case 'besselap (z, p, k)'
app.ZPEdit.Enable = 'on';
app.PEdit.Enable = 'on';
app.KEdit.Enable = 'on';
end
end
% Button pushed function: Calculate
function CalculateButtonPushed(app, event)
app.n = app.OrderSpinner.Value;
app.Wo = str2double(app.WoEditField.Value);
switch app.SyntaxListBox.Value
case 'besself (b, a)'
%if isfinite(app.Wo) % Check if Wo is finite
[app.b, app.a] = besself(round(app.n), app.Wo, app.getFilterType());
app.BEdit.Value = mat2str(app.b);
app.APEdit.Value = mat2str(app.a);
freq_response = freqs(app.b, app.a);
plot(app.FreqAxes, freq_response(:,1), 20*log10(abs(freq_response(:,2))));
%else
% disp('Error: Wo should be a finite value.');
%end
case 'besself (z, p, k)'
%if isfinite(app.Wo)
[app.z, app.p, app.k] = besself(round(app.n), app.Wo, app.getFilterType());
app.ZPEdit.Value = mat2str(app.z);
app.PEdit.Value = mat2str(app.p);
app.KEdit.Value = mat2str(app.k);
freq_response = freqs(app.z, app.p, app.k);
plot(app.FreqAxes, freq_response(:,1), 20*log10(abs(freq_response(:,2))));
%else
% disp('Error: Wo should be a finite value.');
%end
case 'besselap (z, p, k)'
%if isfinite(app.Wo)
[app.z, app.p, app.k] = besself(round(app.n), app.Wo, app.getFilterType());
app.ZPEdit.Value = mat2str(app.z);
app.PEdit.Value = mat2str(app.p);
app.KEdit.Value = mat2str(app.k);
freq_response = freqs(app.z, app.p, app.k);
plot(app.FreqAxes, freq_response(:,1), 20*log10(abs(freq_response(:,2))));
%else
% disp('Error: Wo should be a finite value.');
%end
end
%{
Plotting using freqs()
freqsAxes = app.FreqAxes;
freqsAxes.XLabel.String = 'Frequence (Hz)';
freqsAxes.YLabel.String = 'Magnitude';
[H, W] = freqs(app.b, app.a);
plot(freqsAxes, W, abs(H));
%}
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create FreqAxes
app.FreqAxes = uiaxes(app.UIFigure);
title(app.FreqAxes, 'FreqAxes')
xlabel(app.FreqAxes, 'X')
ylabel(app.FreqAxes, 'Y')
zlabel(app.FreqAxes, 'Z')
app.FreqAxes.FontAngle = 'italic';
app.FreqAxes.FontWeight = 'bold';
app.FreqAxes.Color = [1 1 0.0667];
app.FreqAxes.XGrid = 'on';
app.FreqAxes.YGrid = 'on';
app.FreqAxes.Position = [18 197 404 256];
% Create SyntaxListBox
app.SyntaxListBox = uilistbox(app.UIFigure);
app.SyntaxListBox.Items = {'besself (b, a)', 'besself (z, p, k)', 'besselap(z, p, k)'};
app.SyntaxListBox.ValueChangedFcn = createCallbackFcn(app, @SyntaxListBoxValueChanged, true);
app.SyntaxListBox.Position = [483 363 100 74];
app.SyntaxListBox.Value = 'besself (b, a)';
% Create OrderSpinnerLabel
app.OrderSpinnerLabel = uilabel(app.UIFigure);
app.OrderSpinnerLabel.HorizontalAlignment = 'right';
app.OrderSpinnerLabel.Position = [26 113 77 22];
app.OrderSpinnerLabel.Text = 'OrderSpinner';
% Create OrderSpinner
app.OrderSpinner = uispinner(app.UIFigure);
app.OrderSpinner.Position = [118 113 100 22];
% Create WoEditFieldLabel
app.WoEditFieldLabel = uilabel(app.UIFigure);
app.WoEditFieldLabel.HorizontalAlignment = 'right';
app.WoEditFieldLabel.Position = [30 65 69 22];
app.WoEditFieldLabel.Text = 'WoEditField';
% Create WoEditField
app.WoEditField = uieditfield(app.UIFigure, 'numeric');
app.WoEditField.Position = [114 65 100 22];
% Create FilterTypeGroup
app.FilterTypeGroup = uibuttongroup(app.UIFigure);
app.FilterTypeGroup.TitlePosition = 'centertop';
app.FilterTypeGroup.Title = 'Filter Types';
app.FilterTypeGroup.Position = [471 226 123 106];
% Create LowRadio
app.LowRadio = uiradiobutton(app.FilterTypeGroup);
app.LowRadio.Text = 'low';
app.LowRadio.Position = [11 66 41 22];
app.LowRadio.Value = true;
% Create BandpassRadio
app.BandpassRadio = uiradiobutton(app.FilterTypeGroup);
app.BandpassRadio.Text = 'bandpass';
app.BandpassRadio.Position = [11 45 75 22];
% Create HighRadio
app.HighRadio = uiradiobutton(app.FilterTypeGroup);
app.HighRadio.Text = 'high';
app.HighRadio.Position = [11 21 45 22];
% Create StopRadio
app.StopRadio = uiradiobutton(app.FilterTypeGroup);
app.StopRadio.Text = 'stop';
app.StopRadio.Position = [11 1 46 22];
% Create ZPEditLabel
app.ZPEditLabel = uilabel(app.UIFigure);
app.ZPEditLabel.HorizontalAlignment = 'right';
app.ZPEditLabel.Position = [279 113 41 22];
app.ZPEditLabel.Text = 'ZPEdit';
% Create ZPEdit
app.ZPEdit = uieditfield(app.UIFigure, 'numeric');
app.ZPEdit.Position = [335 113 100 22];
% Create BEditLabel
app.BEditLabel = uilabel(app.UIFigure);
app.BEditLabel.HorizontalAlignment = 'right';
app.BEditLabel.Position = [286 24 34 22];
app.BEditLabel.Text = 'BEdit';
% Create BEdit
app.BEdit = uieditfield(app.UIFigure, 'numeric');
app.BEdit.Position = [335 24 100 22];
% Create Calculate
app.Calculate = uibutton(app.UIFigure, 'push');
app.Calculate.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.Calculate.Position = [483 175 100 23];
app.Calculate.Text = 'Calculate';
% Create APEditEditFieldLabel
app.APEditEditFieldLabel = uilabel(app.UIFigure);
app.APEditEditFieldLabel.HorizontalAlignment = 'right';
app.APEditEditFieldLabel.Position = [279 65 41 22];
app.APEditEditFieldLabel.Text = 'APEdit';
% Create APEdit
app.APEdit = uieditfield(app.UIFigure, 'numeric');
app.APEdit.Position = [335 65 100 22];
% Create PEditEditFieldLabel
app.PEditEditFieldLabel = uilabel(app.UIFigure);
app.PEditEditFieldLabel.HorizontalAlignment = 'right';
app.PEditEditFieldLabel.Position = [473 65 34 22];
app.PEditEditFieldLabel.Text = 'PEdit';
% Create PEdit
app.PEdit = uieditfield(app.UIFigure, 'numeric');
app.PEdit.Position = [522 65 100 22];
% Create KEditEditFieldLabel
app.KEditEditFieldLabel = uilabel(app.UIFigure);
app.KEditEditFieldLabel.HorizontalAlignment = 'right';
app.KEditEditFieldLabel.Position = [473 113 34 22];
app.KEditEditFieldLabel.Text = 'KEdit';
% Create KEdit
app.KEdit = uieditfield(app.UIFigure, 'numeric');
app.KEdit.Position = [522 113 100 22];
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = matlab_app
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur Startup and Shutdown dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!