How to access app designer variables in the script file

36 visualizaciones (últimos 30 días)
Ishita Kaul
Ishita Kaul el 11 de Dic. de 2019
Editada: Adam Danz el 16 de Dic. de 2019
I have created a gui on app designer. In this I am taking input from .csv file which is user input and i want to use this input in my script files. I have tried assign value and making variable global but it doesn't work.
please help.

Respuestas (1)

Adam Danz
Adam Danz el 11 de Dic. de 2019
Don't use assigning() or eval(). That opens a can of worms. Here are two alternatives. Each method includes a demo but you'll have to replace the app name and component handle names.
If your data is already stored in an app component
If the data you loaded is already stored in an app component such as a UITable, you can simply using the app handle to get the handle to the UITable and retreive the data. Here's a demo.
% When you open your app, use the output to get the app handle
appHand = myFakeApp; % This opens your app (insert your app name)
myData = appHand.UITable.Data;
If your data is not already stored in any app components
Use
to store the data to your UIFigure and
to retreive it. Here's a demo where I store a variable named myData to the app figure.
app.UIFigure = uifigure(); % Use your app figure handle instead of creating a figure
setappdata(app.UIFigure,'myData',1:5); % store myData to your app figure
Now you can retreive it from any callback function or from the command window
% When you open your app, use the output to get the app handle
appHand = myFakeApp; % This opens your app (insert your app name)
myData = getappdata(appHand.UIFigure,'myData') % Get the data stored in your app figure
  4 comentarios
Ishita Kaul
Ishita Kaul el 16 de Dic. de 2019
Thanks for your help! I figured it out. I just passed app as a parameter and converted my .m script files to functions and that solved it! Now I can access the variables in my script files.
Adam Danz
Adam Danz el 16 de Dic. de 2019
Editada: Adam Danz el 16 de Dic. de 2019
I see. It would be better practice to pass the data directly from your app into the function rather than passing the app handle into the function. Either way you must extract the data from the app.
One reason why it's better to pass the data rather than passing the app handle is that your function becomes usless outside of that exact app. If you're passing the data as inputs, the function becomes much more versatile.
A second reason is that you don't want many files to have access to your app components. Changes to the app should only happen within the main app file, with few exceptions.
Lastly, it's much easier to read if you extract all of the needed data into a structure variable or cell array or some other container and then pass that to your external function.

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by