MATLAB: Load data from .mat file in App Designer

app designerloadMATLAB

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.

Best Answer

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;