Adupa Vasista
Adupa Vasista

Reputation: 83

Passing variables from GUI to current workspace, MATLAB

I designed an app using app designer, which runs an .m file when pushbutton is pressed. But before that execution, I am browsing some xlsx files and storing there data in some variables, and I am using assignin function to export those variables. These variables are in turn used in the script(.m file), But what I have observed is these variables are present in base workspace which is different from current workspace. Is there any way, that I can pass them to the current workspace.

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

This is just a trail 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

This is the trail.m script file

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

clc;clear

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

disp(name);

Upvotes: 0

Views: 1994

Answers (2)

Cris Luengo
Cris Luengo

Reputation: 60770

This function:

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

can be simplified to:

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

Because the trail command will launch the trail.m script, and scripts share the caller’s workspace. So it will see the local name variable.

In your script trail, make sure you don’t clear all variables: remove the clc;clear line. Otherwise you’re clearing the name variable you are trying to use!

Upvotes: 1

Andrew Janke
Andrew Janke

Reputation: 23898

Use handles, not variables or assignin.

Instead of trying to assign variables directly in a workspace (which is an advanced, brittle technique), I suggest that you use a pass-by-reference handle style object to pass values back and forth between your main function and your GUI callbacks. A containers.Map object will do the trick (since it is a handle), or you can define a custom class using classdef MySharedData < handle. Create the object in your call function and store it in a variable there. Then stick the object in an appdata on one of your figure handles that is visible to your GUI callback functions. To pass data back to the calling/main function, have your GUI callback assign or update values/properties on your shared handle object.

Or you could just stick the values directly in appdata on your GUI handles. They act as handles, too, and this is the traditional Matlab way of passing data back and forth between a calling function and GUI callbacks.

Upvotes: 1

Related Questions