Load data from .mat file in App Designer

71 visualizaciones (últimos 30 días)
Matt
Matt el 22 de Abr. de 2020
Comentada: Ameer Hamza el 22 de Abr. de 2020
I am loading data from a .mat file in App Designer. The user selects the .mat file and I would like to load a variable from the selected file, assigning it to an existing property in the app, like this:
app.x = load([app.path '\' app.file],'inputVariable');
I want to assign inputVariable, a timetable, to app.x, which is used as a timetable elsewhere in the app. But with the load command, app.x becomes a struct with a field named inputVariable that is a timetable. So, app.x.inputVariable is the timetable I end up with. I could create an intermediate variable internal to the function where this is happening and then apply it to the property, like this:
tempX = load([app.path '\' app.file],'inputVariable');
app.x = tempX.inputVariable;
clear tempX;
Or I could load the inputVariable without assigning it so the variable loaded is a timetable rather than a struct with a field that is a timetable:
load([app.path '\' app.file],'inputVariable');
app.x = inputVariable;
clear inputVariable;
This works but it just seemed inefficient. Is there a better way to do this? Thanks.

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 22 de Abr. de 2020
Editada: Ameer Hamza el 22 de Abr. de 2020
Loading into a temporary variable and then assigning it to app.X is the correct method in MATLAB (I guess you made a mistake while writing your second method, check the following lines of code). Don't worry about it being inefficient. MATLAB will not create two copies of the table when you assign it to the field in the app. MATLAB use copy-on-write so only one copy is made of the table in memory: https://www.mathworks.com/help/matlab/matlab_prog/avoid-unnecessary-copies-of-data.html
tempX = load([app.path '\' app.file],'inputVariable');
app.x = tempX.inputVariable;
  2 comentarios
Matt
Matt el 22 de Abr. de 2020
Thanks for the clarification. I updated my example code for posterity.
Ameer Hamza
Ameer Hamza el 22 de Abr. de 2020
I am glad to be of help.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

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

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by