Load data from .mat file in App Designer

73 次查看(过去 30 天)
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.

采纳的回答

Ameer Hamza
Ameer Hamza 2020-4-22
编辑:Ameer Hamza 2020-4-22
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 个评论
Matt
Matt 2020-4-22
Thanks for the clarification. I updated my example code for posterity.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by