Issue with MATLAB App Designer and Undefined Function Error

17 views (last 30 days)
Hi everyone,
I hope you're all doing well. I'm currently facing an issue with my MATLAB App Designer project, and I was hoping to get some assistance.
I'm using the MATLAB App Designer to create an app, and I have a set of three .m files - myfun.m, constraints.m, and optimize.m. The optimize.m file is a function file containing the following code:
function [x, fval, exitflag, output, lambda, grad, hessian] = optimize(x0)
options = optimoptions('ga');
%% Modify options setting
options = optimoptions(options, 'Display', 'off');
options = optimoptions(options, 'PlotFcn', { @optimplotx @optimplotfunccount @optimplotfval @optimplotconstrviolation @optimplotstepsize @optimplotfirstorderopt });
[x, fval, exitflag, output, lambda, grad, hessian] = ...
fmincon(@myfun, x0, [], [], [], [], [], [], @constraints, options);
In my App Designer, I have set up multiple EditFields and a button. Under the button, I use the following code:
% Button pushed function: OptimizeButton
function OptimizeButtonPushed(app, event)
z(1) = str2double(app.EditField1.Value);
z(2) = str2double(app.EditField2.Value);
z(3) = str2double(app.EditField3.Value);
z(4) = str2double(app.EditField4.Value);
z(5) = str2double(app.EditField5.Value);
z(6) = str2double(app.EditField6.Value);
z(7) = str2double(app.EditField7.Value);
z(8) = str2double(app.EditField8.Value);
x0 = [z(1) z(2) z(3) z(4) z(5) z(6) z(7) z(8)];
[x, fval, exitflag, output, lambda, grad, hessian] = optimize(x0);
end
However, when I run the app and click on "Optimize" Button, it stops and shows "Undefined function 'optimize' for input arguments of type 'double.'"
I've made sure that all three .m files and the app design file are in the same directory. Can anyone help me figure out what might be causing this issue?
Thank you in advance for your assistance!
Best regards.

Accepted Answer

Taylor
Taylor on 10 Jan 2024
It is generally advised to keep all necessary code contained within the app when possible. I would recommend implementing your functions as helper functions within the app.
  2 Comments
Shariful Islam
Shariful Islam on 10 Jan 2024
Hi @Taylor, thanks for your reply.
How can I seamlessly integrate three MATLAB function files (myfun.m, constraints.m, and optimize.m) into my App Designer project to resolve this issue?
Taylor
Taylor on 10 Jan 2024
I would add them as Private Functions. The link in my previous comment has detailed instructions on how to implement the functions within your app. One important difference between functions in apps vs those in scripts is that the first input must be the "app" handle.

Sign in to comment.

More Answers (0)

Categories

Find more on Develop Apps Using App Designer in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!