Using fminsearch in the app designer

3 ビュー (過去 30 日間)
David
David 2022 年 7 月 7 日
コメント済み: David 2022 年 7 月 8 日
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
Michael Van de Graaff 2022 年 7 月 7 日
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 件のコメント
David
David 2022 年 7 月 8 日
Thank you for your comments, you're right!

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeDevelop 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