Using a trained regression model in app designer

35 views (last 30 days)
Orhi
Orhi on 13 May 2022
Edited: Drew on 27 Feb 2023
I used regression learner to create a trained model that can predict future inflation based on inflation data of the past 10 years. I can use this model to predict new data in command window by typing "trainedModel.predictFcn(T)".
I'm trying to do exact same thing in the app designer. I created a private property called model and loaded the trained model into that property in the startupFcn function.
app.model = load("Model\model2.mat");
I put a button in my app to prediction based on the data in the app, I have this code in its function
app.EditField.Value = app.model.predictFcn(data);
but it doesn't work, I get this error "unrecognized field name 'predictFcn". I'm new user and I really don't have any idea what I'm doing wrong.

Answers (1)

Drew
Drew on 27 Feb 2023
Edited: Drew on 27 Feb 2023
Short answer
The short answer is that there is probably another layer in the app.model object, so instead of
app.EditField.Value = app.model.predictFcn(data);
you need something like
app.EditField.Value = app.model.trainedModel.predictFcn(data);
Elaboration
As an example, I loaded the fisheriris data, launched Classification Learner, built the default tree model, exported it as trainedModel, and saved it into model2.mat. Note that Classification Learner and Regression Learner work similarly with respect to this behavior, so an example on either will illustrate the point nicely.
t=readtable("fisheriris.csv")
classficationLearner % in the app, load the fisher iris data, build default tree, and export it as trainedModel
save model2.mat trainedModel
Then I launched AppDesigner, added to the startupFcn as you did
app.model = load("model2.mat");
then set a breakpoint at something like your second line of code in a callback for a button press, and I see this when inspecting app.model at the commandline:
K>> app.model
ans =
struct with fields:
trainedModel: [1×1 struct]
K>> app.model.trainedModel
ans =
struct with fields:
predictFcn: @(x)exportableModel.predictFcn(predictorExtractionFcn(x))
RequiredVariables: {'PetalLength' 'PetalWidth' 'SepalLength' 'SepalWidth'}
ClassificationTree: [1×1 ClassificationTree]
About: 'This struct is a trained model exported from Classification Learner R2022b.'
HowToPredict: 'To make predictions on a new table, T, use: ↵ yfit = c.predictFcn(T) ↵replacing 'c' with the name of the variable that is this struct, e.g. 'trainedModel'. ↵ ↵The table, T, must contain the variables returned by: ↵ c.RequiredVariables ↵Variable formats (e.g. matrix/vector, datatype) must match the original training data. ↵Additional variables are ignored. ↵ ↵For more information, see How to predict using an exported model.'
This app designer code worked to test random fisheriris observations. For this simple demo the results are just displayed to the command window with the "disp" function.
classdef tutorialApp < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
TestRandomPointButton matlab.ui.control.Button
end
properties (Access = public)
model % model saved from classification Learner
t % fisher iris data for testing
index % which obvervation index was tested
predictedLabel % which label was predicted
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
app.model=load("model2.mat");
app.t=readtable('fisheriris.csv');
end
% Button pushed function: TestRandomPointButton
function TestRandomPointButtonPushed(app, event)
app.index=randi(150);
app.predictedLabel=app.model.trainedModel.predictFcn(app.t(app.index,:));
disp(app.predictedLabel)
disp(app.index)
end
end
If this answered your question, please remember to accept the answer.

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!