[英]how to pass data from one app to another in matlab gui (using app designer)
我正在設計一個 Gui,我將在第一個應用程序上輸入一些內容,並希望它出現在第二個應用程序上。 這兩個應用程序相互鏈接,即應用程序 1 是 GUI 的第一頁,應用程序 2 是第二頁。
當 App1 持有 App2 的句柄時,這很簡單。
應用程序1:
properties (Access = private)
hApp2 % Handle to app2
end
從 App1 執行 App2:
% Code that executes after component creation
function startupFcn(app)
app.hApp2 = App2;
end
按下按鈕時,從 App1 在 App2 中設置 label:
% Button button pushed function
function ButtonButtonPushed(app)
app.hApp2.Label.Text = 'Button Pressed';
end
App1完整代碼(示例):
classdef App1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure % UI Figure
Button matlab.ui.control.Button % Button
end
properties (Access = private)
hApp2 % Handle to app2
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.hApp2 = App2;
end
% Button button pushed function
function ButtonButtonPushed(app)
app.hApp2.Label.Text = 'Button Pressed';
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
% Create Button
app.Button = uibutton(app.UIFigure, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonButtonPushed);
app.Button.Position = [231 261 153 61];
end
end
methods (Access = public)
% Construct app
function app = App1()
% Create and configure 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
App2完整代碼(示例):
classdef App2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure % UI Figure
Label matlab.ui.control.Label % Label
end
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.Position = [236 338 63 21];
end
end
methods (Access = public)
% Construct app
function app = App2()
% Create and configure 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.