matlab - 将变量从 GUI 传递到当前工作区,MATLAB
问题描述
我使用应用程序设计器设计了一个应用程序,它在按下按钮时运行一个 .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
这是 trail.m 脚本文件
%%%%%%%%%%%%%%%%%%%% trail.m %%%%%%%%%%%%%%%%%%%%%%%
clc;clear
suma = 90;
sumb = 100;
total = suma+sumb;
disp(name);
解决方案
使用句柄,而不是变量或assignin
.
与其尝试直接在工作区中分配变量(这是一种高级而脆弱的技术),我建议您使用传递引用handle
样式的对象在主函数和 GUI 回调之间来回传递值。一个containers.Map
对象可以解决问题(因为它是一个句柄),或者您可以使用classdef MySharedData < handle
. 在您的调用函数中创建对象并将其存储在其中的变量中。然后将对象粘贴到您的图形句柄之一上的应用数据中,该句柄对您的 GUI 回调函数可见。要将数据传回调用/主函数,请让您的 GUI 回调分配或更新共享句柄对象上的值/属性。
或者,您可以直接将值粘贴到 GUI 句柄上的 appdata 中。它们也充当句柄,这是 Matlab 在调用函数和 GUI 回调之间来回传递数据的传统方式。
推荐阅读
- java - 未来取消不适用于多个线程的 executorService
- amazon-web-services - AWS CORS 启用 - 需要在哪里启用?圣杯?动态数据库?还是S3?
- javafx - 如何定义滚动窗格的值?
- obd-ii - 需要有关 OBD 扫描仪的信息
- android - Android,无法转换为 Fragment
- java - Android Studio 3 的代理问题
- symfony - 学说复合键和一对多
- android - 使用架构组件构建的应用程序是否使其成为“MVVM”,如果是,应用程序的哪些部分与 MVVM 的哪一层相关?
- android - 在特定日期之后设置每周警报
- node.js - Node/Express 好的错误处理方法?