MATLAB Answers

[App Designer] Can an app accept variables from the base workspace as input arguments?

25 views (last 30 days)
Chris Naber
Chris Naber on 15 Aug 2019
Commented: Adam Danz on 19 Aug 2019
I'm working on an App Designer app. I'd like it to be able to pull in certain variables from the base workspace when the app is loaded and use those to determine what app startup functions are necessary. (In other words, if X and Y are already stored in certain variables in a base function, pre-populate the values of X and Y in the app on startup.)
I have input arguments set up in the app via startupFcn(). From what I can tell, even after the app is opened, if you want the app to run with certain input arguments, you have to use the Run command in App Designer and add the appropriate input arguments at runtime. I would like to combine these steps and just call the app to run and enter the input arguments during the appdesigner() call. Is this possible?
Potential workaround: For the variables I want to share with the app, make them globals. Then the app would be able to interact with them immediately, assuming the app knows the right variable name to use. Would that work?

  1 Comment

Adam Danz
Adam Danz on 19 Aug 2019
Definitely don't do the global variable idea. Avoid global variables.
Here's a warning about using eval(). This method is looked down upon.
In your case, here are some potential problems using the eval() method
  • Just because "importantVariable" exists in the base workspace doesn't mean it's the variable you expect it to be. You should do additional tests to make sure the variable fits your expectations (size, numel, isnumeric, etc...)
  • Let's say "importantVariable" does exis and passes your tests but has crazy data. Will you know where those data that appear in your app come from? One way around that is to include some kind of announcement that data from the base workspace will be loaded so if things look funky at least you'll know that the data were not generated from within the app.
  • Let's say "importantVariable" was generated in your base workspace properly. Then you wanted to test some things so you removed the NaNs, extracted column 3, or rounded all negative values. Later you open your app and the altered "importantVariable" is now loaded into your app. That data no longer represent what it should but since the alterations were 225 commands ago, you've forgotten.
  • Data loaded from eval is not traceable. If you run into an unexpected error from within the app or if the results don't look right, the problem is no longer contained and there is no way to look back at where the values in "importantVariable" went wrong.
Those are all real problems people face when using eval(). One alternative is to save imporant variables from within the function that generated them. The variables can be saved to a mat file and set to read-only (date stamps should also be saved). Your app can detect the presence of the mat file and the load in the variables. That's more contained than using eval().

Sign in to comment.

Answers (1)

Subhadeep Koley
Subhadeep Koley on 19 Aug 2019
Edited: Subhadeep Koley on 19 Aug 2019
You can use evalin to achieve the same.
  • In the App Designer right click on the UIFigure, hover over 'callbacks', select ‘Add startupFcn callback'
  • Paste the following code inside the startupFcn function
localX= evalin('base','X');
localY= evalin('base','Y');
% Example Usage of localX & localY
f = app.UIFigure;
f.Position=[localX localY 640 480];
  • Save & Run the App.
  • Values of ‘X’ and ‘Y’ are stored in ‘localX’ & ‘localY’.


Sign in to comment.

Sign in to answer this question.

Translated by