Using fminsearch in the app designer

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

采纳的回答

Walter Roberson
Walter Roberson 2022-7-7
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);

更多回答(1 个)

Michael Van de Graaff
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

类别

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

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by