Load data from .mat file in App Designer

98 views (last 30 days)
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.

Accepted Answer

Ameer Hamza
Ameer Hamza on 22 Apr 2020
Edited: Ameer Hamza on 22 Apr 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 Comments
Matt
Matt on 22 Apr 2020
Thanks for the clarification. I updated my example code for posterity.

Sign in to comment.

More Answers (0)

Categories

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

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!