How to troubleshoot "Warning: Unable to save App Designer app object. Save not supported for matlab.apps.AppBase objects." Issue in App Designer?

39 次查看(过去 30 天)
I was working on a MATLAB App that saves the app properties to a mat file in UIFigureCloseRequest callback, and loads the saved properties from the mat file in startUpFcn. This was working until I made some changes to the app.
Since then, everytime I close the app, I get
"Warning: Unable to save App Designer app object. Save not supported for matlab.apps.AppBase objects." ,
Also, everytime I repen the app, I get
"Error using startupFcn Unable to load App Designer app object. Load not supported for matlab.apps.AppBase objects."
When I remove the mat file, then I don't get the error when I open the app, but I get the warning when I close the app.
Here is the app properties
properties (Access = private)
Bot
Messages
end
Here is how the properties are initialized
sfun = @(x)streamTokens(app,x);
app.Bot = openAIChat("You are an AI assistant.",StreamFun=sfun);
app.Messages = openAIMessages;
This is the used to save the properties in UITree.
s = struct('bot',app.Bot,'messages',app.Messages);
uitreenode(app.Tree,NodeData=s,Text="My Chat");
Here is the starupFcn, which restore the data into UITree.
function startupFcn(app)
if isempty(app.Tree.Children)
uitreenode(app.Tree,"Text","New Chat")
end
if isfile("ChatData.mat")
% this line triggers the error
load("ChatData.mat","PreviousChats");
for ii = 1:height(PreviousChats)
topic = PreviousChats.topic(ii);
s = PreviousChats.chat_history(ii);
uitreenode(app.Tree,NodeData=s,Text=topic)
end
end
end
Here is the UIFigureCloseRequest, which saves the text and Node Data from UITree to a mat file.
function UIFigureCloseRequest(app, event)
chats = app.Tree.Children(2:end);
PreviousChats = table;
PreviousChats.topic = arrayfun(@(x) string(x.Text), chats);
PreviousChats.chat_history = arrayfun(@(x) x.NodeData, chats);
isValid = arrayfun(@(x) numel(x.messages.Messages), PreviousChats.chat_history) >= 2;
PreviousChats(~isValid,:) = [];
if numel(PreviousChats.topic) > 0
% this line triggers the warning
save("ChatData.mat","PreviousChats")
else
if isfile("ChatData.mat")
delete("ChatData.mat");
end
end
delete(app)
end

采纳的回答

Toshiaki Takeuchi
The issue was caused by the function handle that included the reference to appbase.
sfun = @(x)streamTokens(app,x);
This was passed as part of the object
app.Bot = openAIChat("You are an AI assistant.",StreamFun=sfun);
And this was saved to the node data in UITree
s = struct('bot',app.Bot,'messages',app.Messages);
uitreenode(app.Tree,NodeData=s,Text="My Chat");
Then the node data was saved to the mat file.
PreviousChats.chat_history = arrayfun(@(x) x.NodeData, chats);
For future reference, the way I've troubleshooted this issue was to just load the MAT file at the command line (i.e take the app out of the picture) and inspect what its contents are. I check for any unexpected variables, and if nothing looks off, I then start to save the variables in small batches into a different MAT file until I see the warning get triggered. This helps track down the offending variable you can then dig into to see what it contains.
In this particular example, mat file contained node data, which contained a struct with two fields: bot and messages. I extracted the data saved in the bot field and I got the error message. When I saved the data in the messages field, I didn't. So I know that app reference in the bot field, which contained the openAIChat object. When I looked at the code that initialized the object, I saw that I was passing a function handle that contained the reference to the app.

更多回答(0 个)

类别

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

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by