Using fminsearch in the app designer
조회 수: 2 (최근 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)
댓글 수: 0
채택된 답변
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);
댓글 수: 0
추가 답변 (1개)
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Develop Apps Using App Designer에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!