将变量从GUI传递到当前工作空间,MATLAB

时间:2020-06-20 20:08:18

标签: matlab user-interface

我使用应用设计器设计了一个应用,当按下按钮时,该应用运行一个.m文件。但是在执行该操作之前,我正在浏览一些xlsx文件并将数据存储在某些变量中,并且正在使用assignin 函数来导出这些变量。这些变量又在脚本(.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

这是trace.m脚本文件

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

clc;clear

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

disp(name);

2 个答案:

答案 0 :(得分:1)

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

建议不要使用直接引用handle样式对象在主对象之间来回传递值,而不是尝试直接在工作空间中分配变量(这是一种先进的,易碎的技术)。函数和您的GUI回调。 containers.Map对象可以解决这个问题(因为它是一个句柄),或者您可以使用classdef MySharedData < handle定义一个自定义类。在调用函数中创建对象,并将其存储在其中的变量中。然后,将该对象粘贴到GUI回调函数可见的图形手柄之一的appdata中。要将数据传递回调用/主函数,请让GUI回调分配或更新共享句柄对象上的值/属性。

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

答案 1 :(得分:1)

此功能:

   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变量!

相关问题