Trying to make an app in app designer to predict using trained NN model from classification learner but fails to classify the data

조회 수: 2 (최근 30일)
I managed to import data in csv and show them in the application table, I manage to load the model with the second button but when I try to predict the data I get an error saying that it does not recognize the function I use to predict the data normally with the model.
I have tried to predict using the way they advise in other cases:
Ypred = predict(model, data);
but I got the error:
Undefined function 'predict' for input arguments of type 'table'.
I suspect the latter is due to the fact that the neural network is stored as a structure where the function used to make the prediction is a function handle.
here is the code of the app so far:
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
predictButton matlab.ui.control.Button
ImportmodelButton_2 matlab.ui.control.Button
ImportdataButton matlab.ui.control.Button
UITable2 matlab.ui.control.Table
UITable matlab.ui.control.Table
end
properties (Access = private)
data_imp % Description
pred_NN2lys
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: ImportdataButton
function ImportdataButtonPushed(app, event)
% Load the trained classifier
[filename, filepath] = uigetfile({'*.csv'; '*.xlsx'}, 'Select Data File');
data = readtable(fullfile(filepath, filename));
app.data_imp = data;
app.UITable.Data = data;
end
% Button pushed function: ImportmodelButton_2
function ImportmodelButton_2Pushed(app, event)
[filename, filepath] = uigetfile('*.mat', 'Select Data File');
NN2lys = load(fullfile(filepath, filename));
app.pred_NN2lys = NN2lys
end
% Clicked callback: UITable
function UITableClicked(app, event)
displayRow = event.InteractionInformation.DisplayRow;
displayColumn = event.InteractionInformation.DisplayColumn;
end
% Button pushed function: predictButton
function predictButtonPushed(app, event)
input = app.data_imp;
YPred = app.pred_NN2lys.predictFnc(input);
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.Scrollable = 'on';
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.BackgroundColor = [1 1 1;0.9412 0.9412 0.9412];
app.UITable.ColumnName = {'Ag'; 'Bi'; 'Cu'; 'Fe'; 'Ni'; 'Pb'; 'Pd'; 'Pt'; 'Sb'; 'Te'};
app.UITable.RowName = {};
app.UITable.ColumnEditable = true;
app.UITable.ClickedFcn = createCallbackFcn(app, @UITableClicked, true);
app.UITable.Position = [16 31 408 419];
% Create UITable2
app.UITable2 = uitable(app.UIFigure);
app.UITable2.ColumnName = {'Deposit'};
app.UITable2.RowName = {};
app.UITable2.ColumnEditable = true;
app.UITable2.Position = [554 31 75 419];
% Create ImportdataButton
app.ImportdataButton = uibutton(app.UIFigure, 'push');
app.ImportdataButton.ButtonPushedFcn = createCallbackFcn(app, @ImportdataButtonPushed, true);
app.ImportdataButton.Position = [436 272 100 23];
app.ImportdataButton.Text = 'Import data';
% Create ImportmodelButton_2
app.ImportmodelButton_2 = uibutton(app.UIFigure, 'push');
app.ImportmodelButton_2.ButtonPushedFcn = createCallbackFcn(app, @ImportmodelButton_2Pushed, true);
app.ImportmodelButton_2.Position = [436 230 100 23];
app.ImportmodelButton_2.Text = 'Import model';
% Create predictButton
app.predictButton = uibutton(app.UIFigure, 'push');
app.predictButton.ButtonPushedFcn = createCallbackFcn(app, @predictButtonPushed, true);
app.predictButton.Position = [436 184 100 23];
app.predictButton.Text = 'predict';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = app1
runningApp = getRunningApp(app);
% Check for running singleton app
if isempty(runningApp)
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
else
% Focus the running singleton app
figure(runningApp.UIFigure)
app = runningApp;
end
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
  댓글 수: 2
Christopher McCausland
Christopher McCausland 2023년 10월 26일
Just a comment,
If you are using a line like this;
predict = predict(model, data);
MATLAB, or any other programing langauge will error as 'predict' is now both a function and a variable. This type of overshadowing is handled by matlab in that the function cannot run until the variable is cleared from memory. You can read more about function precedence order here.
Christopher
Angel Verbel
Angel Verbel 2023년 10월 26일
Hi Christopher,
My mistake, I changed the name of the variable when I copied it here because it was originally in Spanish, so they are different in the original scrip.
Thank you for bringing this point to my attention.
Angel

댓글을 달려면 로그인하십시오.

답변 (2개)

Githin George
Githin George 2023년 10월 30일
Hello,
My understanding is that you are having a model exported from the Classification Learner app named ‘NN2lys’ and you are facing an issue when using the “predictFcn” method of this model in “predictButtonPushed” callback.
The “predictFcn” requires the data with the same format and data type as the training data used in the Classification Learner app. Use the following command on MATLAB command window to get the right instructions on how to pass data to the “predictFcn” method.
NN2lys.HowToPredict
As an alternative, the struct ‘NN2lys’ also contains the actual trained model which can be used directly with the “predict” function.
PS: There is a typo in Line 2 of the “predictButtonPushed” callback. Please change it to the following:
YPred = app.pred_NN2lys.predictFcn(input);
I hope this helps.

Federico Mazzoli
Federico Mazzoli 2024년 5월 12일
Hi Angel,
I have the same problem.
You solved it somehow?
Federico

카테고리

Help CenterFile Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by