Unable to save/load objects (uitree uitable) created by App designer ? (R2019a)

9 次查看(过去 30 天)
Hi, I'm using App Designer to build an app with a uitree and a uitable. The tree/table is editable in the app during runtime. I want the edited tree/table to be saved to a local place, and also be able to be loaded in the future. So I created the save/load buttons to save/load the uitree/uitable as the following (Tree as example):
function Button_Save(app, event) % save the app.Tree to TreeSave.mat
ProgramTree=app.Tree;
uisave('ProgramTree','TreeSave')
end
function Button_Load(app, event) % load and assign
ProgramTree= load('TreeSave.mat');
app.Tree=ProgramTree;
end
But there are warning "Unable to save/load the object created by App designer" and error "Invalid data type. The data type must be matlab.ui.container.Tree or can be converted to matlab.ui.container.Tree", and the load function is not working. It seems Matlab has banned the save/load function for objects created by Appdesigner. I'm wondering if there is a way to walk around this, or is there anyway to extract the information of uitree to some other type of file, maybe .xls, for reuse? Thank you for reading.
  3 个评论
Adam
Adam 2019-10-22
编辑:Adam 2019-10-22
Saving and loading ui components is generally a bad idea. Try saving the raw data and recreate (or re-fill if it still exists) the component with the loaded raw data instead.
Inso
Inso 2019-10-23
Thanks. I totally agree with you and I'm trying to write a function to extract the uitree information (treenode relationship and userdata). But I think it'll take me some time to achieve, since the uitree is customized. Is there any existing functions for reference? :)

请先登录,再进行评论。

回答(1 个)

Inso
Inso 2019-10-25
Problem solved. Write down to share.
Basically, my original codes have two malfunctions. One is the usage of 'load':
ProgramTree= load('TreeSave.mat');
After assignment, the 'ProgramTree' becomes a struct that contains the tree, instead of the tree itself. 'ProgramTree.ProgramTree' is the 'real' tree. Thus the tree is not loaded correctly. 'load(filename,variables)' or 'load(filename)' is suggested.
The second problem is the "not supported functionality of saving/loading matlab.Apps.AppBase object" when excecute
app.Tree=ProgramTree;
I found it's not only a warning but also may cause problem in node selection of the loaded tree. So I tried to save the children of the tree instead:
f = app.Tree.Children; % Use a loop and save the Children in a struct, if there is more than one first order child.
save('DeviceSave.mat','f')
Then load the Children and transplant them to the tree, as needed:
delete(app.Tree.Children);
load('DeviceSave.mat','f')
f.Parent=app.Tree;
So far the functions work OK, and there is no warning any more.
  1 个评论
Vaibhav Deshmukh
Vaibhav Deshmukh 2024-5-1
编辑:Vaibhav Deshmukh 2024-5-1
Last line of code actually assigns the loaded tree (f) to the app. I would've expected the code to be
app.Tree= f.Parent
but it is the opposite. Thanks for this explanation. Very helpful.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Environment and Settings 的更多信息

产品


版本

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by