How to store data from DialogApp in CallingApp while closing the DialogApp in App Designer?

5 views (last 30 days)
Hi,
I have a Push Button function in an App, that opens a DialogApp with two spinners and runs a seperate Matlab function thereafter.
What I want from the DialogApp is to store these two values in the main App/insert them into the function and then close the Dialog Window before/while the function runs and populates the axes in the main App.
I managed to create a Push Button in the main App, that opens both the dialog window and runs the function, but it does not populate the function with the two desired values from the DialogApp.
function PushButtonPushed(app, event)
% Disable Plot Options button while dialog is open
app.PushButton.Enable = 'off';
% Call dialog box with input values
dby=1;compass=0;
app.DialogApp = DialogApp(app,dby,compass);
[app.x,app.y,app.z,app.N,app.E,app.Up,app.P,app.Q,app.Upak,app.Lpak,app.t2,app.t4]=my_function(dby,compass);
plot(app.pq_axes, app.t4,app.P)
plot(app.packer_axes, app.t4,app.Upak,app.t4,app.Lpak)
app.PushButton.Enable = 'on';
end
Can someone tell me how to store the values from the DialogApp in the main App so that the function can use them?
Thank you!
  1 Comment
F S
F S on 3 Sep 2021
Edited: F S on 3 Sep 2021
Below I also posted the code from the DialogApp to better understand. Of course the way it's working right now is that both the DialogApp and my function take the default values I defined as 1 and 0 and run with that, without any influnence from the spinner of the DialogApp. I'm aware, there might be more than one problem with it. But what I really don't manage to fix is how to get the values from the DialogApp into my function.
classdef DialogApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
OKButton matlab.ui.control.Button
toolorientationSpinner matlab.ui.control.Spinner
toolorientationSpinnerLabel matlab.ui.control.Label
speedupdownsampledatabySpinner matlab.ui.control.Spinner
speedupdownsampledatabySpinnerLabel matlab.ui.control.Label
end
properties (Access = private)
CallingApp % Description
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app, mainapp, dby, compass)
app.CallingApp=mainapp;
end
% Button pushed function: OKButton
function OKButtonPushed(app, event)
delete(app)
end
% Value changed function: speedupdownsampledatabySpinner
function speedupdownsampledatabySpinnerValueChanged(app, event)
dby = app.speedupdownsampledatabySpinner.Value;
end
% Value changed function: toolorientationSpinner
function toolorientationSpinnerValueChanged(app, event)
compass = app.toolorientationSpinner.Value;
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 254 226];
app.UIFigure.Name = 'MATLAB App';
% Create speedupdownsampledatabySpinnerLabel
app.speedupdownsampledatabySpinnerLabel = uilabel(app.UIFigure);
app.speedupdownsampledatabySpinnerLabel.HorizontalAlignment = 'center';
app.speedupdownsampledatabySpinnerLabel.Position = [40 173 176 45];
app.speedupdownsampledatabySpinnerLabel.Text = {'speed up '; '(downsample data by:)'};
% Create speedupdownsampledatabySpinner
app.speedupdownsampledatabySpinner = uispinner(app.UIFigure);
app.speedupdownsampledatabySpinner.Limits = [1 10000];
app.speedupdownsampledatabySpinner.RoundFractionalValues = 'on';
app.speedupdownsampledatabySpinner.ValueChangedFcn = createCallbackFcn(app, @speedupdownsampledatabySpinnerValueChanged, true);
app.speedupdownsampledatabySpinner.HorizontalAlignment = 'center';
app.speedupdownsampledatabySpinner.Position = [84 155 103 22];
app.speedupdownsampledatabySpinner.Value = 1;
% Create toolorientationSpinnerLabel
app.toolorientationSpinnerLabel = uilabel(app.UIFigure);
app.toolorientationSpinnerLabel.HorizontalAlignment = 'right';
app.toolorientationSpinnerLabel.Position = [86 80 84 22];
app.toolorientationSpinnerLabel.Text = 'tool orientation';
% Create toolorientationSpinner
app.toolorientationSpinner = uispinner(app.UIFigure);
app.toolorientationSpinner.Limits = [0 360];
app.toolorientationSpinner.ValueChangedFcn = createCallbackFcn(app, @toolorientationSpinnerValueChanged, true);
app.toolorientationSpinner.Position = [86 105 84 22];
% Create OKButton
app.OKButton = uibutton(app.UIFigure, 'push');
app.OKButton.ButtonPushedFcn = createCallbackFcn(app, @OKButtonPushed, true);
app.OKButton.FontWeight = 'bold';
app.OKButton.Position = [52 18 151 39];
app.OKButton.Text = 'OK';
% 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 = DialogApp(varargin)
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
% Execute the startup function
runStartupFcn(app, @(app)startupFcn(app, varargin{:}))
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

Sign in to comment.

Answers (1)

Adam Danz
Adam Danz on 7 Sep 2021
Edited: Adam Danz on 8 Sep 2021
Follow this demo (attached). If you have any trouble, let us know where you're at and we can help you get unstuck.
  • App1 generates a random number when opened and contains a button to open App2.
  • The starting function for App2 includes a 2nd input for the App1 handle.
  • When App2 is opened, it uses the App1 handle to extract the random number from App1 and copies it to App2.
Here are the steps taken for App2 to get data from App1
1. In App2: Add a startup function and add a 2nd input to the startup function. The first condition below closes App2 if it's not opened with a second input. This condition could be even more strict and require the 2nd input to be the handle specifically to App1. Next, the random number of App1 is extracted and copied to App2.
% Code that executes after component creation
function app2StartupFcn(app, app1Handle)
% Requires 2nd input; throw error if missing or empty
if nargin < 2 || isempty(app2Handle)
close(app.UIFigure)
error('App2 must be opened by App1.')
end
% Get app1 random variable value
app1RandVar = app1Handle.RandomnumberEditField.Value;
% Set app2
app.Randomnumberfromapp1EditField.Value = app1RandVar;
end
2. In App1: The push button is assigned a callback function to open App2 and includes the handle to App1 in the 2nd input.
function Openapp2ButtonPushed(app, event)
app2(app)
end
  2 Comments
F S
F S on 10 Sep 2021
Edited: F S on 10 Sep 2021
Hi Adam, thank you for your answer! This is not exactly doing what I'm looking for however. To describe it with your two example apps, I would like them to do the following:
  1. initially, app1 contains NO data value, but only the push button function
  2. when pushing the button in app1, it opens app2, where I can insert 1 (or in my case 2) value(s), which get(s) saved and displayed by app1
  3. app2 closes, the values remains in app1
in all my trys i was loosing the value from app2 when closing it, so really what I need is it to be stored in app1 permanently beforehand. is this possible?
Adam Danz
Adam Danz on 10 Sep 2021
You can still use the logic in my answer with some minor additions.
Step 2 of my answer opens app2 and passes the handle from app1 to app2.
When you insert a value into app2, the callback function that responds to the the changed value can store the data in app1 using the app handle.

Sign in to comment.

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!