【问题标题】:Passing variables from GUI to current workspace, MATLAB将变量从 GUI 传递到当前工作区,MATLAB
【发布时间】:2020-10-10 22:30:23
【问题描述】:

我使用应用程序设计器设计了一个应用程序,它在按下按钮时运行一个 .m 文件。但在执行之前,我正在浏览一些 xlsx 文件并将数据存储在一些变量中,并且我正在使用 assignin function 导出这些变量。这些变量依次在脚本(.m 文件)中使用,但我观察到这些变量存在于与当前工作区不同的基本工作区中。有什么办法可以将它们传递到当前工作区。

assignin("base",'name',name2)

这只是一个轨迹 GUI

classdef app < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure        matlab.ui.Figure
        ContinueButton  matlab.ui.control.Button
        Button          matlab.ui.control.Button
        Button2         matlab.ui.control.Button
        Button3         matlab.ui.control.Button
    end

    
  
    

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

        % Code that executes after component creation
        function startupFcn(app)
            %app.ds
            %uiwait(app.UIFigure);
        end

        % Button pushed function: ContinueButton
        function ContinueButtonPushed(app, event)
            name = 'string';
            assignin("base",'name',name)
            run("trail.m")
            closereq
            %set(handle.Operation)
        end

        % Close request function: UIFigure
        function UIFigureCloseRequest(app, event)
            delete(app)
            %uiresume(app.UIFigure);
        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';
            app.UIFigure.CloseRequestFcn = createCallbackFcn(app, @UIFigureCloseRequest, true);
            app.UIFigure.Pointer = 'hand';

            % Create ContinueButton
            app.ContinueButton = uibutton(app.UIFigure, 'push');
            app.ContinueButton.ButtonPushedFcn = createCallbackFcn(app, @ContinueButtonPushed, true);
            app.ContinueButton.Position = [164 106 262 92];
            app.ContinueButton.Text = 'Continue';

            % Create Button
            app.Button = uibutton(app.UIFigure, 'push');
            app.Button.Position = [454 254 100 22];

            % Create Button2
            app.Button2 = uibutton(app.UIFigure, 'push');
            app.Button2.Position = [104 254 100 22];
            app.Button2.Text = 'Button2';

            % Create Button3
            app.Button3 = uibutton(app.UIFigure, 'push');
            app.Button3.Position = [301 335 100 22];
            app.Button3.Text = 'Button3';

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

            % 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

这是 trail.m 脚本文件

%%%%%%%%%%%%%%%%%%%% trail.m %%%%%%%%%%%%%%%%%%%%%%%

clc;clear

suma = 90;
sumb = 100;
total = suma+sumb;

disp(name);

【问题讨论】:

  • “传递到当前工作区”是什么意思?您想从基础工作区获取这些变量吗?您可能希望将它们放入 GUI 的上下文中。不了解程序的结构很难提出具体的建议。
  • @CrisLuengo 抱歉,含糊不清。传递到当前工作区意味着,当在 GUI 中生成变量时,我想将它们推送到我当前的工作区中。明白了吗。如果没有,我可以附上一些代码。
  • 但是当 GUI 回调运行时,您当前的工作区是什么?这是回调的工作区!当用户在 GUI 中按下按钮时正在运行的任何功能都会暂停,因此不再是当前功能。我认为添加一些演示您的一般架构的最小代码会很有用。
  • @CrisLuengo 更新了问题
  • @CrisLuengo 在这里不要太技术化,但是:当回调运行时,当前工作区是回调的工作区,但同样重要的是,“调用者”工作区不能保证是 GUI -builder 的工作区,或任何特别的东西! GUI 调用堆栈与主 M 代码执行调用堆栈分离。

标签: matlab user-interface


【解决方案1】:

使用句柄,而不是变量或assignin

我建议您使用 pass-by-reference handle 样式对象在您的 main 函数和您的 GUI 回调。 containers.Map 对象可以解决问题(因为它是句柄),或者您可以使用 classdef MySharedData &lt; handle 定义自定义类。在您的调用函数中创建对象并将其存储在其中的变量中。然后将对象粘贴到您的图形句柄之一上的应用数据中,该句柄对您的 GUI 回调函数可见。要将数据传回调用/主函数,请让您的 GUI 回调分配或更新共享句柄对象上的值/属性。

或者您可以直接将值粘贴到 GUI 句柄上的 appdata 中。它们也充当句柄,这是 Matlab 在调用函数和 GUI 回调之间来回传递数据的传统方式。

【讨论】:

  • 感谢您的回复,但坦率地说不知道句柄,我有点了解这个概念,但我担心我无法编码。我通过添加代码更新了我的问题,请让我知道我需要添加的内容。
  • 在 Matlab 中,handle 对象只是具有传递引用行为的事物,而不是像普通 Matlab 值那样传递值的写时复制行为。如果您想了解基本的 Matlab 编程,恐怕这是您必须了解的内容。我要去睡觉了;明天我会审核您的修改。感谢您根据反馈更新您的问题!
【解决方案2】:

这个函数:

   function ContinueButtonPushed(app, event)
        name = 'string';
        assignin("base",'name',name)
        run("trail.m")
        closereq
        %set(handle.Operation)
    end

可以简化为:

   function ContinueButtonPushed(app, event)
        name = 'string';
        trail
        closereq
        %set(handle.Operation)
    end

因为trail 命令将启动trail.m 脚本,并且脚本共享调用者的工作空间。所以它会看到本地的name 变量。

在您的脚本 trail 中,确保您没有清除所有变量:删除 clc;clear 行。否则,您将清除您尝试使用的 name 变量!

【讨论】:

  • 感谢它现在的工作。但是我不能在工作区中使用我的任何变量,因为所有变量都在调用者的工作区中。如果我们像@Andrewjanke 在他的回答中所说的那样尝试,也许我们可以进入当前的工作区。我能知道怎么做吗。谢谢
猜你喜欢
  • 2015-10-21
  • 2011-02-15
  • 1970-01-01
  • 2015-03-29
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多