After load ANN data in App Designer, how to predict from any input from user, then show the output? (callback is applied)

6 views (last 30 days)
My ANN coding is first being run in the matlab .m file before loaded into App Designer.
The neural network data has two input columns (A and B) and one output column (C). (Refer to first picture)
After run in matlab, the output shown in command window is in two columns (refer to the second picture).
An app designer is made (refer to third picture) to present this neural network better and the data is loaded succesfully. However, to make the 'predict' button works, the coding for prediction keeps showing error (refer to the last picture). Tried a lot of things and the error is always for this prediction line only. This error pops up after two inputs are inserted (as number) and the predict button is pressed. Callback is made for predict button only.
Any suggestion to solve this problem?
The coding for 'predict' button:
% Button pushed function: PredictButton
function PredictButtonPushed(app, event)
% Get user input from edit fields
InputRating = str2double(app.InputRatingEditField.Value);
InputCurrent = str2double(app.InputCurrentEditField.Value);
%%inputTable = table(InputRating, InputCurrent);
% Create a matrix with the input data
inputData = [InputRating, InputCurrent];
% Perform prediction using your ANN model
%%prediction = predict(app.net,InputRating,InputCurrent);
prediction = predict(app.net, inputData);
% Display the prediction result in the output text
app.OutputText.Value = sprintf('Prediction: %.2f', prediction);
% Display the prediction result in the output text
app.OutputRatingEditField.Value = string(prediction);
end
end

Accepted Answer

Cris LaPierre
Cris LaPierre on 23 Nov 2023
To make predictions, I suggest using your model like a function, meaning you pass the input to your model directly.
prediction = app.net(inputData)
  3 Comments
Cris LaPierre
Cris LaPierre on 24 Nov 2023
Edited: Cris LaPierre on 24 Nov 2023
For app designer, my suggestion is to use an Edit Field (Numeric) when you are collecting or displaying numbers. Then you don't have to worry about using str2double or string.
Other than that, your app designer code looks correct. You've correctly created app properties, and you've added most of the components your code uses. I only noticed that, based on your code, there is no OutputText component. You might have changed the name of your label, so it might still work for you.
For labels, the display property is Text, not Value
Also, I didn't see an OutputRatingEditField in your public app properties. Make sure you add this component or your code will return an error.
When you load a mat file using the syntax you are using in your app - S = load(___) - it loads the variable(s) to a structure. So app.net is a structure, not a network. You must go one more level to get to your network: app.net.net
Finally, notice in your first code, your input to net is x, a 2x480 array. So row 1 is the first input value, and row 2 is the 2nd input. Each column is a separate prediction. Make sure your inputs are a column vector, not a row vector (use a semicolon to separate then instead of a comma).
I only had to make changes to your Predict button callback to build a working app.
function PredictButtonPushed(app, event)
% Get user input from edit fields
InputRating = app.InputRatingEditField.Value; % removed str2double
InputCurrent = app.InputCurrentEditField.Value;
% Create a matrix with the input data
inputData = [InputRating; InputCurrent]; % turned into a column vector
% Perform prediction using your ANN model
%prediction = predict(app.net, InputData);
prediction = app.net.net(inputData); % access desired field in structure
% Display the prediction result in the output text
app.OutputLabel.Text = sprintf('Prediction: %.2f', prediction); % assign to Text property
% Display the prediction result in the output text
app.OutputRatingEditField.Value = prediction; % removed string
end
Aida Zulaikha
Aida Zulaikha on 25 Nov 2023
Hello, I followed your recommendations and that works smoothly. Thank you very much for the help!
I appreciate your time and effort a lot! Thank you!

Sign in to comment.

More Answers (0)

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!