Issue with MATLAB App Designer and Undefined Function Error
22 次查看(过去 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.
0 个评论
采纳的回答
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 个评论
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 Center 和 File 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!