Issue with MATLAB App Designer and Undefined Function Error

조회 수: 19 (최근 30일)
Shariful Islam
Shariful Islam 2024년 1월 10일
댓글: Taylor 2024년 1월 10일
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