Using fminsearch in the app designer

7 visualizaciones (últimos 30 días)
David
David el 7 de Jul. de 2022
Comentada: David el 8 de Jul. de 2022
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)
myrealdata
end
methods (Access = public)
function results = minimizethis(app,c)
for i = 1:length(app.myrealdata)
calculation=c(1)+c(2);
end
results=(sum(app.myrealdata-calculation).^2)^0.5
end
end
% 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);
[output]=fminsearch(app.minimizethis,data,options);
[app.EditField app.EditField_2]=deal(output)

Respuesta aceptada

Walter Roberson
Walter Roberson el 7 de Jul. de 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);

Más respuestas (1)

Michael Van de Graaff
Michael Van de Graaff el 7 de Jul. de 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
end
Also, I suggest you add a semicolon to the results assignment in minimizethis
  1 comentario
David
David el 8 de Jul. de 2022
Thank you for your comments, you're right!

Iniciar sesión para comentar.

Categorías

Más información sobre Develop Apps Using App Designer en Help Center y File Exchange.

Productos


Versión

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by