how to create a sinusoid calculator in app designer in matlab

To create a sinusoid calculator in App Designer in MATLAB, you can follow these steps:

  1. Open MATLAB and create a new App Designer by typing appdesigner in the command window.
  2. In App Designer, drag and drop two components:
    • Two edit fields for inputting amplitude and frequency
    • An axes component for plotting the sinusoid
  3. Add a button for triggering the calculation and plotting.
  4. In the button's callback function, write code to calculate the sinusoid based on the entered amplitude and frequency values, and plot the result on the axes.

Here's a basic example code to get you started:

main.m
% App Designer Code
classdef SinusoidCalculator < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure    matlab.ui.Figure
        AmplitudeEditField  matlab.ui.control.NumericEditField
        FrequencyEditField  matlab.ui.control.NumericEditField
        PlotAxes    matlab.ui.control.UIAxes
        CalculateButton matlab.ui.control.Button
    end

    methods (Access = private)

        % Code executed after component creation
        function startupFcn(app)
            % Set up the initial plot
            plot(app.PlotAxes, 0, 0, 'b-')
            xlabel(app.PlotAxes, 'Time')
            ylabel(app.PlotAxes, 'Amplitude')
        end

        % Button pushed function
        function CalculateButtonPushed(app, event)
            % Get amplitude and frequency values
            amplitude = app.AmplitudeEditField.Value;
            frequency = app.FrequencyEditField.Value;

            % Generate sinusoidal data
            t = 0:0.01:2*pi;
            y = amplitude * sin(2*pi*frequency*t);

            % Plot the sinusoid
            plot(app.PlotAxes, t, y, 'r-')
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = SinusoidCalculator
            % Create UIFigure and components
            app.createComponents()

            % Register the app with App Designer
            app.registerApp(app.UIFigure)

            % Execute the startup function
            runStartupFcn(app, @startupFcn)
        end

        % Components initialization
        function createComponents(app)
            % Create UIFigure and components
            app.UIFigure = uifigure;
            app.UIFigure.Position = [100 100 400 300];
            app.UIFigure.Name = 'Sinusoid Calculator';

            app.AmplitudeEditField = uieditfield(app.UIFigure, 'numeric');
            app.AmplitudeEditField.Position = [100 250 100 22];
            app.AmplitudeEditField.Value = 1;

            app.FrequencyEditField = uieditfield(app.UIFigure, 'numeric');
            app.FrequencyEditField.Position = [100 200 100 22];
            app.FrequencyEditField.Value = 1;

            app.PlotAxes = uiaxes(app.UIFigure);
            app.PlotAxes.Position = [250 50 300 200];

            app.CalculateButton = uibutton(app.UIFigure, 'push');
            app.CalculateButton.Position = [100 150 100 22];
            app.CalculateButton.Text = 'Calculate';
            app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
        end
    end

    % App startup and display
    methods (Access = public)

        % Start the app
        function run(app)
            % Display the app
            movegui(app.UIFigure, 'center')
            app.UIFigure.Visible = 'on';
        end

    end
end
2875 chars
90 lines

This code creates an App Designer with amplitude and frequency input fields, a plot to display the sinusoid, and a button to trigger the calculation and plotting of the sinusoid. You can customize this code further based on your requirements.

related categories

gistlibby LogSnag