1

我正在 MATLAB 中开发一个应用程序,并使用应用程序设计来构建它。我添加了一个文本区域元素,在其中向用户显示消息(与命令窗口类似)。在应用程序中,用户可以按下按钮,触发要执行的功能,在这些功能中,我希望能够在此文本区域元素中显示一些消息。

这是我用来在此文本区域中显示文本的代码示例。我使用计数器在列表中添加文本并模拟显示而不覆盖以前的消息。

% display execution message
app.nb_Text_stock                                   = app.nb_Text_stock + 1;
app.OutputStatusTextArea.Value(app.nb_Text_stock)   = {'My test here'};

如您所见,我需要 app 元素。然后我可以将它一直传递给函数,直到我需要显示文本的级别,但我真正的问题是,我可以从函数内部访问 app 元素而不将其作为参数传递吗?我想这样做的原因是我还有一个非 GUI 版本的脚本,我无法将 app 作为参数传递。因此,为了使事情更简单,我想要一个参数 GUI = 1 或 0,然后基于该显示,如果 GUI = 0,则在命令窗口中显示,如果 GUI = 1,则在 GUI 的文本区域中显示。但为此我需要从我的函数内部访问 app 元素。有没有合适的方法来做到这一点?或者你对这个问题的另一种方法有什么建议吗?

4

2 回答 2

0

.Parent如果您有任何图形对象的句柄,则可以使用和.Children字段(例如hObject.Parent.Parent.Children(3).String = 'foo')在同一图中找到几乎任何其他对象,也可以使用ancestor. 如果您没有任何对象句柄,则可以使用findall,但这需要一些方法来识别正确的图形/控件。这可以使用他们的Tag字段来完成,但需要事先指定。

于 2020-02-11T12:27:37.433 回答
0

您可以使用 setappdata 存储对象app,并使用getappdata获取对象:

  • Store appin startupFcnfunction(组件创建后执行的代码):通过在“代码视图”中
    添加startupFcn回调来添加。

    % Code that executes after component creation
    function startupFcn(app)
        % Store app in the root object (setappdata(groot, 'my_app', app) also works).
        setappdata(0, 'my_app', app)
    end
    
  • app从任何函数 读取对象:

    app = getappdata(0, 'my_app');
    

笔记:

  • 这不是一个好的编码习惯。

你应该做什么:

function NonGuiFun()
app = app1();
app.func();

你要求做什么:

function NonGuiFun()

% Get app object (assuming `app` GUI is already open)
app = getappdata(0, 'my_app');

if ~isempty(app)
    app.func();
end

app1这是我用于测试 的整个类代码(大部分是自动生成的):

classdef app1 < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure              matlab.ui.Figure
        Button                matlab.ui.control.StateButton
        TextAreaLabel         matlab.ui.control.Label
        OutputStatusTextArea  matlab.ui.control.TextArea
    end


    properties (Access = private)
        nb_Text_stock = 0; % Description
    end

    methods (Access = public)

        function results = func(app)
            app.nb_Text_stock = app.nb_Text_stock + 1;
            app.OutputStatusTextArea.Value(app.nb_Text_stock)   = {num2str(app.nb_Text_stock)};
        end
    end


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

        % Code that executes after component creation
        function startupFcn(app)
            setappdata(0, 'my_app', app)
        end

        % Value changed function: Button
        function ButtonValueChanged(app, event)
            value = app.Button.Value;
            func(app);
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            setappdata(0, 'my_app', [])
            delete(app)

        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 = 'UI Figure';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);

            % Create Button
            app.Button = uibutton(app.UIFigure, 'state');
            app.Button.ValueChangedFcn = createCallbackFcn(app, @ButtonValueChanged, true);
            app.Button.Text = 'Button';
            app.Button.Position = [214 295 214 85];

            % Create TextAreaLabel
            app.TextAreaLabel = uilabel(app.UIFigure);
            app.TextAreaLabel.HorizontalAlignment = 'right';
            app.TextAreaLabel.Position = [210 211 56 22];
            app.TextAreaLabel.Text = 'Text Area';

            % Create OutputStatusTextArea
            app.OutputStatusTextArea = uitextarea(app.UIFigure);
            app.OutputStatusTextArea.Position = [281 175 150 60];

            % 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 = app1

            % Create UIFigure and components
            createComponents(app)

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

            % 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.UIFigure)
        end
    end
end

注意UIFigureCloseRequest执行:setappdata(0, 'my_app', []).

于 2020-02-11T17:46:09.993 回答