Issue with MATLAB App Designer and Undefined Function Error

17 次查看(过去 30 天)
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.

采纳的回答

Taylor
Taylor 2024-1-10
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 个评论
Shariful Islam
Shariful Islam 2024-1-10
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 2024-1-10
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Develop Apps Using App Designer 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by