Save / Load a uitree object from app designer

19 ビュー (過去 30 日間)
Yi Wan
Yi Wan 2019 年 1 月 3 日
編集済み: Raph 2022 年 12 月 24 日
Hi there,
I'm recently writing an app that contains a uitree. The tree is editable in the app during runtime. I want my app to have the function to save the edited tree to a local place, and also able to load an existing tree from a local directory. So I did the following:
In the UI I created a 'save' button and a 'load' button with call backs as below:
function SaveProgramButtonPushed(app, event) % save button callback
if ~isempty(app.Tree.Children)
program=app.Tree;
uisave('program','myProgramme');
else
uialert(app.SIIRD_Robot_UI_v2,'There is no programme to save!','Save Programme Alert');
end
end
function LoadProgramButtonPushed(app, event) % load button callback
answer=uiconfirm(app.SIIRD_Robot_UI_v2,'Load new programme now? The current programme will be discarded','New Programme Alert');
if strcmp(answer,'OK')
[file,path]=uigetfile('*.mat');
if file~=0
load(fullfile(path,file),'program');
app.newprogrammeLabel.Text=file;
app.Tree=program;
app.Tree.Parent=app.ProgramTab;
app.Tree.SelectionChangedFcn = createCallbackFcn(app, @TreeSelectionChanged, true);
end
end
end
However, during runtime, although the load and save function still can work when I click the load / save button, warnings appear as follows:
"Warning: Functionality not supported with figures created with the uifigure function. For more information, see Graphics Support in App Designer. "
I wonder is there anything I can do to walkaround this? Or saving / loading a UI compenent using uisave / uiload is not a good way? Is there any better way to do this?
  1 件のコメント
Inso
Inso 2019 年 10 月 15 日
Hi, thanks for your question, since I'm trying the same thing.
However, when I load the saved .mat file and run the code:
app.Tree=program;
It shows that 'program' is an undefined function or varable. Error :(
What could be the problem? Thx

サインインしてコメントする。

回答 (2 件)

Kevin Chng
Kevin Chng 2019 年 1 月 23 日
編集済み: Kevin Chng 2019 年 1 月 23 日
Hi Yi Wan,
"Warning: Functionality not supported with figures created with the uifigure function. For more information, see Graphics Support in App Designer. "
It is not the main reason to cause your code not working. It is a warning to remind instead of error.
First, your uisave should be working fine and able to create the mat.file for you.
uisave('program','myProgramme');
Second, It is succesful to load the file as your expected, however, your code below is not working.
app.Tree=program;
Third, hence, I recommend you to add a new delete button to clear to content of the tree first, then later load the mat.file into the tree
function d(app, event) % Button Callback Function
delete(app.Tree.Children)
end
Fourth, now we load the mat. file and get the content add into the tree by following method
function LoadProgramButtonPushed(app, event) % load button callback
[file,path]=uigetfile('*.mat');
if file~=0
load(fullfile(path,file));
category = t.Children;
for i=1:1:length(category)
tree1(i) = uitreenode(app.Tree);
tree1(i).Text = category(i).Text;
content = category(i).Children;
for j =1:1:length(content)
tree2(i,j) = uitreenode(tree1(i));
tree2(i,j).Text = content(j).Text;
end
%if the branching is still needed, you need to add another loop here as what i did above
%get the children of content
%loop them
end
end
end
Now, i guess it is working well with your case.
  2 件のコメント
Yi Wan
Yi Wan 2019 年 1 月 28 日
Hi Kevin,
Yes, with the warning the programme is still able to run. And code:
app.Tree=program;
is also working fine, regardless of deleting the tree before or not.
My question here is whether there is a decent way to prevent the warning from prompting out. Currently I used a evalin('base','clc') to clear the warnings, but it is not decent.
And I want to ask whether uisave and uigetfile are currently fully supported features for uitree objects, or is there any other decent way to achieve the same function without warnings popping up.
Anyway, thanks for your help a lot!
Kevin Chng
Kevin Chng 2019 年 1 月 29 日
%put the code before the warning
warning('off')
it will turn the warning off.
whether uisave and uigetfile are currently fully supported features for uitree objects, or is there any other decent way to achieve the same function without warnings popping up.
Refer to your statement above, i'm not sure. So far, this is the only way I know for App Designer. May be others might able to answer it.

サインインしてコメントする。


Raph
Raph 2022 年 12 月 24 日
編集済み: Raph 2022 年 12 月 24 日
Easiest is to clear the current tree and then define the parent of the nodes as the current tree.
RM
children_nodes = app.Tree.Children;
t = struct;
t.children_nodes =children_nodes;
save('file.mat','t');% save children_nodes
load('file.mat');% load children_nodes
for i = length(app.Tree.Children):-1:1
app.Tree.Children(i).delete;
end
children_nodes = t.children_nodes;
for i = 1:length(children_nodes)
children_nodes(i).Parent = app.Tree;
end

カテゴリ

Help Center および File ExchangeDevelop uifigure-Based Apps についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by