Using fminsearch in the app designer

2 views (last 30 days)
I am trying to use fminsearch to find the minimum of a function inside an app created with app designer. However, I am having problems trying to use it. The function in question needs other data that is present in the app public properties, but when calling fminsearch, it seems that it does not pass correctly the values it must modify for the minimization.
I've created a simpler example that has the same problem. When the "Button" is pressed it will try to minimize the function, but the error "not enough input arguments" comes up, as if fminsearch is not passing the values of "data" to the function.
Is there any way to do this?
properties (Access = public)
methods (Access = public)
function results = minimizethis(app,c)
for i = 1:length(app.myrealdata)
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
data=[app.EditField, app.EditField_2];
app.myrealdata=[1 2 3 4];
options= optimset('Display', 'notify','MaxIter',200);
[app.EditField app.EditField_2]=deal(output)

Accepted Answer

Walter Roberson
Walter Roberson on 7 Jul 2022
The first parameter to fminsearch() needs to be a function handle.
With the code you have posted, app.minimizethis would be executed with no parameters, and would need to return a function handle.
You probably need something closer to
obj = @(c) app.minimizethis(c)
[output]=fminsearch(obj, data, options);

More Answers (1)

Michael Van de Graaff
Michael Van de Graaff on 7 Jul 2022
As written, data is a 1x2 array of EditField objects. You need to get the actual number values
You may want to replace the edit fields with numeric edit fields, I downloaded your MWE app and notices you seems to be using text edit fields (not the same as TextArea!) instead of numeric edit fields.
Walter's point is also correct, I got no errors with the following code.
function ButtonPushed(app, event)
data=[str2num(app.EditField.Value), str2num(app.EditField_2.Value)];
app.myrealdata=[1 2 3 4];
options= optimset('Display', 'notify','MaxIter',200);
[output]=fminsearch(@(indata) app.minimizethis(indata),data,options);
% [app.EditField app.EditField_2]=deal(output) % I did't
% update this
Also, I suggest you add a semicolon to the results assignment in minimizethis
  1 Comment
David on 8 Jul 2022
Thank you for your comments, you're right!

Sign in to comment.




Community Treasure Hunt

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

Start Hunting!