matlab AppDesigner与轴限制相关的问题

mpbci0fu  于 2022-11-15  发布在  Matlab
关注(0)|答案(1)|浏览(192)

下面是我的代码

classdef Average_Pepsi < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        StockPricesAnalysisUIFigure  matlab.ui.Figure
        DefaultSettingsButton        matlab.ui.control.Button
        AveragingwindowSlider        matlab.ui.control.Slider
        AveragingwindowSliderLabel   matlab.ui.control.Label
        StopKnob                     matlab.ui.control.Knob
        StopKnobLabel                matlab.ui.control.Label
        StartKnob                    matlab.ui.control.Knob
        StartKnobLabel               matlab.ui.control.Label
        AverageOptionsButtonGroup    matlab.ui.container.ButtonGroup
        BothButton                   matlab.ui.control.RadioButton
        MonthlyAverageButton         matlab.ui.control.RadioButton
        MonthlyButton                matlab.ui.control.RadioButton
        UIAxes                       matlab.ui.control.UIAxes
    end

    
    properties (Access = private)
        ShowAverage
        ShowMonthly
        AveragingWindow
        Averages
        StartYear
        StopYear
       Date % Description
       Stock_Price
    end
    
    methods (Access = private)
        
        function plot_data(app)
            xlim(app.UIAxes,[app.StartYear app.StopYear])
              xlim(app.UIAxes,'manual')
            if app.ShowMonthly
                plot(app.UIAxes,app.Date,app.Stock_Price)
                hold(app.UIAxes,'on')
            end
            if app.ShowAverage
                 plot(app.UIAxes,app.Date,app.Averages,'r','LineWidth',2)
                 
            end
            hold(app.UIAxes,'off')
        end
        
        
        function computeAverage(app)
            app.Averages = movmean(app.Stock_Price,app.AveragingWindow);
            
        end
        
        function setDefaults(app)
            app.AveragingwindowSlider.Value=app.AveragingwindowSlider.Limits(2) /2;
            app.BothButton.Value =true;
            app.MonthlyButton.Value =false;
            app.MonthlyAverageButton.Value =false;
            app.StartKnob.Value =app.StartKnob.Limits(1);
            app.StopKnob.Value =app.StopKnob.Limits(2);
            app.AveragingWindow =app.AveragingwindowSlider.Value;
            app.ShowAverage =app.MonthlyAverageButton.Value ||app.BothButton.Value;
            app.ShowMonthly =app.MonthlyButton.Value ||app.BothButton.Value;
            app.StartYear =app.StartKnob.Value;
            app.StopYear =app.StopKnob.Value;
            app.computeAverage()
            app.plot_data()
            
            
            
        end
    end
    

    % Callbacks that handle component events
    methods (Access = private)

        % Code that executes after component creation
        function startupFcn(app)
            load PEP.mat PEP
            app.Date =PEP.Date;
            app.Stock_Price = PEP.Open;
            app.setDefaults()
           % app.StartYear =app.StartKnob.Value;
            %app.StopYear =app.StopKnob.Value;
            %app.AveragingWindow =app.AveragingwindowSlider.Value;
            %app.ShowAverage =app.MonthlyAverageButton.Value ||app.BothButton.Value;
            %app.ShowMonthly =app.MonthlyButton.Value ||app.BothButton.Value;
           %app.computeAverage()
           %app.plot_data()
        end

        % Selection changed function: AverageOptionsButtonGroup
        function AverageOptionsButtonGroupSelectionChanged(app, event)
            app.ShowAverage =app.MonthlyAverageButton.Value ||app.BothButton.Value;
            app.ShowMonthly =app.MonthlyButton.Value ||app.BothButton.Value;
             app.plot_data()
        end

        % Value changing function: StartKnob
        function StartKnobValueChanging(app, event)
            app.StartYear = event.Value;
           if app.StartYear > app.StopYear-1
                app.StopYear =min([app.StartYear+1,app.StopKnob.Limits(2)]);
                app.StopKnob.Value =app.StopYear;
                app.StartYear =app.StopYear-1;
                app.StartKnob.Value =app.StartYear;
                
            end
            app.plot_data()
            
        end

        % Value changing function: StopKnob
        function StopKnobValueChanging(app, event)
           app.StopYear = event.Value;
            if app.StopYear < app.StartYear+1
                app.StartYear =max([app.StopYear-1,app.StartKnob.Limits(1)]);
                app.StartKnob.Value =app.StartYear;
                app.StopYear =app.StartYear+1;
                app.StopKnob.Value =app.StopYear;
                
            end
            app.plot_data()
            
        end

        % Value changing function: AveragingwindowSlider
        function AveragingwindowSliderValueChanging(app, event)
            app.AveragingWindow = event.Value;
            app.computeAverage()
            app.plot_data()
            
        end

        % Button pushed function: DefaultSettingsButton
        function DefaultSettingsButtonPushed(app, event)
            app.setDefaults()
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create StockPricesAnalysisUIFigure and hide until all components are created
            app.StockPricesAnalysisUIFigure = uifigure('Visible', 'off');
            app.StockPricesAnalysisUIFigure.Color = [0.4667 0.6745 0.1882];
            app.StockPricesAnalysisUIFigure.Position = [100 100 640 480];
            app.StockPricesAnalysisUIFigure.Name = 'Stock Prices Analysis';

            % Create UIAxes
            app.UIAxes = uiaxes(app.StockPricesAnalysisUIFigure);
            title(app.UIAxes, 'Pepsi Average')
            xlabel(app.UIAxes, {'Year'; ''})
            ylabel(app.UIAxes, 'Average Pepsi Stock Prices')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.Position = [1 247 565 234];

            % Create AverageOptionsButtonGroup
            app.AverageOptionsButtonGroup = uibuttongroup(app.StockPricesAnalysisUIFigure);
            app.AverageOptionsButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @AverageOptionsButtonGroupSelectionChanged, true);
            app.AverageOptionsButtonGroup.Title = 'Average Options';
            app.AverageOptionsButtonGroup.Position = [10 142 123 106];

            % Create MonthlyButton
            app.MonthlyButton = uiradiobutton(app.AverageOptionsButtonGroup);
            app.MonthlyButton.Text = 'Monthly';
            app.MonthlyButton.Position = [11 60 64 22];

            % Create MonthlyAverageButton
            app.MonthlyAverageButton = uiradiobutton(app.AverageOptionsButtonGroup);
            app.MonthlyAverageButton.Text = 'Monthly Average';
            app.MonthlyAverageButton.Position = [11 38 111 22];

            % Create BothButton
            app.BothButton = uiradiobutton(app.AverageOptionsButtonGroup);
            app.BothButton.Text = 'Both';
            app.BothButton.Position = [11 16 65 22];
            app.BothButton.Value = true;

            % Create StartKnobLabel
            app.StartKnobLabel = uilabel(app.StockPricesAnalysisUIFigure);
            app.StartKnobLabel.HorizontalAlignment = 'center';
            app.StartKnobLabel.Position = [195 121 31 22];
            app.StartKnobLabel.Text = 'Start';

            % Create StartKnob
            app.StartKnob = uiknob(app.StockPricesAnalysisUIFigure, 'continuous');
            app.StartKnob.Limits = [2000 2022];
            app.StartKnob.ValueChangingFcn = createCallbackFcn(app, @StartKnobValueChanging, true);
            app.StartKnob.Position = [180 167 60 60];
            app.StartKnob.Value = 2000;

            % Create StopKnobLabel
            app.StopKnobLabel = uilabel(app.StockPricesAnalysisUIFigure);
            app.StopKnobLabel.HorizontalAlignment = 'center';
            app.StopKnobLabel.Position = [354 221 30 22];
            app.StopKnobLabel.Text = 'Stop';

            % Create StopKnob
            app.StopKnob = uiknob(app.StockPricesAnalysisUIFigure, 'continuous');
            app.StopKnob.Limits = [2000 2022];
            app.StopKnob.MajorTicks = [2001 2005 2009 2013 2017 2022];
            app.StopKnob.ValueChangingFcn = createCallbackFcn(app, @StopKnobValueChanging, true);
            app.StopKnob.Position = [338 167 60 60];
            app.StopKnob.Value = 2000;

            % Create AveragingwindowSliderLabel
            app.AveragingwindowSliderLabel = uilabel(app.StockPricesAnalysisUIFigure);
            app.AveragingwindowSliderLabel.HorizontalAlignment = 'right';
            app.AveragingwindowSliderLabel.Position = [471 199 103 22];
            app.AveragingwindowSliderLabel.Text = 'Averaging window';

            % Create AveragingwindowSlider
            app.AveragingwindowSlider = uislider(app.StockPricesAnalysisUIFigure);
            app.AveragingwindowSlider.Limits = [4 12];
            app.AveragingwindowSlider.ValueChangingFcn = createCallbackFcn(app, @AveragingwindowSliderValueChanging, true);
            app.AveragingwindowSlider.Position = [456 197 150 3];
            app.AveragingwindowSlider.Value = 4;

            % Create DefaultSettingsButton
            app.DefaultSettingsButton = uibutton(app.StockPricesAnalysisUIFigure, 'push');
            app.DefaultSettingsButton.ButtonPushedFcn = createCallbackFcn(app, @DefaultSettingsButtonPushed, true);
            app.DefaultSettingsButton.Position = [300 44 100 22];
            app.DefaultSettingsButton.Text = 'Default Settings';

            % Show the figure after all components are created
            app.StockPricesAnalysisUIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = Average_Pepsi

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.StockPricesAnalysisUIFigure)

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

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.StockPricesAnalysisUIFigure)
        end
    end
end

并有相应的结果:

例如,当我尝试单击任何按钮时,例如MonthlyAverage或Change滑动按钮,我会遇到以下错误:

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AverageOptionsButtonGroupSelectionChanged (line 97)
             app.plot_data()
 
Error using matlab.ui.control.internal.model.AbstractMutualExclusiveComponent/set.Value (line 151)
Error while evaluating ButtonGroup SelectionChangedFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AverageOptionsButtonGroupSelectionChanged (line 97)
             app.plot_data()
 
Error using matlab.ui.control.internal.model.AbstractMutualExclusiveComponent/set.Value (line 151)
Error while evaluating ButtonGroup SelectionChangedFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StartKnobValueChanging (line 110)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StartKnobValueChanging (line 110)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StartKnobValueChanging (line 110)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StopKnobValueChanging (line 124)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StopKnobValueChanging (line 124)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StopKnobValueChanging (line 124)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StopKnobValueChanging (line 124)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StopKnobValueChanging (line 124)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/StopKnobValueChanging (line 124)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Knob PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AveragingwindowSliderValueChanging (line 132)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Slider PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AveragingwindowSliderValueChanging (line 132)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Slider PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AveragingwindowSliderValueChanging (line 132)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Slider PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AveragingwindowSliderValueChanging (line 132)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Slider PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AveragingwindowSliderValueChanging (line 132)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Slider PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/AveragingwindowSliderValueChanging (line 132)
            app.plot_data()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Slider PrivateValueChangingFcn.

Error using xlim (line 31)
Limits must be a 2-element vector of increasing datetime values.

Error in Average_Pepsi/plot_data (line 35)
            xlim(app.UIAxes,[app.StartYear app.StopYear])

Error in Average_Pepsi/setDefaults (line 67)
            app.plot_data()

Error in Average_Pepsi/DefaultSettingsButtonPushed (line 138)
            app.setDefaults()
 
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 410)
Error while evaluating Button PrivateButtonPushedFcn.

但我有检查这些错误的代码,其中一个是:

function StartKnobValueChanging(app, event)
            app.StartYear = event.Value;
           if app.StartYear > app.StopYear-1
                app.StopYear =min([app.StartYear+1,app.StopKnob.Limits(2)]);
                app.StopKnob.Value =app.StopYear;
                app.StartYear =app.StopYear-1;
                app.StartKnob.Value =app.StartYear;
                
            end
            app.plot_data()
            
        end

另一个是这样的:

function StopKnobValueChanging(app, event)
           app.StopYear = event.Value;
            if app.StopYear < app.StartYear+1
                app.StartYear =max([app.StopYear-1,app.StartKnob.Limits(1)]);
                app.StartKnob.Value =app.StartYear;
                app.StopYear =app.StartYear+1;
                app.StopKnob.Value =app.StopYear;
                
            end
            app.plot_data()
            
        end

4.我不明白这个错误的原因

3hvapo4f

3hvapo4f1#

您正在逐个分配这两个限制。如果将它们赋值在一起,应该不会再出现错误:
Axes.XLim=Start End

相关问题