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

51 vues (au cours des 30 derniers jours)
Chris Naber
Chris Naber le 15 Août 2019
Commenté : Ivan Demec le 17 Oct 2023
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 commentaire
Adam Danz
Adam Danz le 19 Août 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().

Connectez-vous pour commenter.

Réponses (2)

Subhadeep Koley
Subhadeep Koley le 19 Août 2019
Modifié(e) : Subhadeep Koley le 19 Août 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’.
  1 commentaire
Ivan Demec
Ivan Demec le 17 Oct 2023
Is there any way to list all of the variables from the base workspace?
Thanks.
Ivan

Connectez-vous pour commenter.


Lucille Cazenave
Lucille Cazenave le 29 Sep 2021
You can also save your variable in a '.mat' file at the end of your function and then load this '.mat' file it at the start of your other function and use that variable stored in the file directly with the same variable name. It is a quick fix, that works well.

Catégories

En savoir plus sur Develop Apps Using App Designer dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by