【发布时间】: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