MATLAB: Deploy a model created by Classification learner as an excel add-in

add-inappclassificationdeploydepolytoolexcelexportlearnerslibrarycompilerMATLAB Compilermodeltrainedmodel

How to deploy the model generated from Classification Learner App as an excel add-in?

Best Answer

To deploy the model, you would have to save the model from Classification Learners App. Please follow the steps below:
1. Once you have trained the model in the app, save that model using "Export Model" function from the app. This should generate a struct variable for the model in MATLAB workspace. Let it be "trainedModel"
2. Save the "trainedModel" struct into a "MAT" file. Lets say, "model.mat"
3. Write a "M" file to be deployed as an Excel Add-in which should perform the following steps:
  • Load "model.mat"
  • Convert the input data from excel into "table" as model requires table datatype to execute.
  • Extract "ClassificationTree" from the model.
  • Perform a call to "predict" function passing "ClassificationTree" and the data from excel as Input parameters.
  • The call to predict function would return a prediction based on input data and the trained model. Return this value to Excel.
NOTE:
  • You cannot use "predictFcn" which is inside the "trainedModel" in a deployed target.
  • Since there are more than one predict function in all the MATLAB toolboxes, you would have to use "function pragma" to let the dependency analyzer know which "predict" function to use. In this case we have to use "predict" function from "Classification Tree" method.
  • The sequence of data input from Excel is important as wrong sequence might skew the predictions.
  • The input data requirements may differ based on the model being used. In this case, the model requires data to be in form of table.
  • For an SVM classifier that uses classificationECOC, the "function pragma" would be as follows:
*
%#function ClassificationECOC
Below is a sample code for M file which can be used to deploy an Excel Add-in:
% This function takes "ValidationData" as an input from Excel and
% returns "yfit" which a prediction based on the "trainedModel" from "model.mat"
function yfit = runModelFromMATLAB(ValidationData)
%#function ClassificationTree % declares which "predict" function to use
load model.mat; % trained model from Classification Learners App
ModTableData = ValidationData;
% convert data to table
ModTableData = cell2table(ModTableData, 'VariableNames',...
{'status','trial_devcount','errorcnts','trial_avgviewedmins',...
'trialviewedmins','trial_avgbitrate'});
% get classification tree from the model and call predict using this tree
ModelClassTree = trainedModel.ClassificationTree;
yfit = predict(ModelClassTree, ModTableData);
yfit = {char(yfit)};