How to call user defined cost function through MATLAB GUI?

1 回表示 (過去 30 日間)
Shrivardhan Suryawanshi
Shrivardhan Suryawanshi 2017 年 2 月 19 日
回答済み: Nirav Sharda 2017 年 2 月 22 日
I have a push button in my GUI, where I can give path of the cost function through following code:
function Get_Cost_Function_Callback(hObject, eventdata, handles)
% hObject handle to Get_Cost_Function (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[FileName,PathName] = uigetfile('*.m','Select the MATLAB code file');
[pathstr,name,ext] = fileparts(FileName);
handles.name=char(name);
guidata(hObject, handles);
Now whatever cost function given by the user is stored in 'handles.name'.But the problem is that I can't call the same function in other button's callback where I have following code:
x = PSOcodeRTDA(Name,MI,np,C1,C2,w,wdamp)
Here 'Name' has the string of function but it is not working.Instead of 'Name' if I use '@cost_function1a'then only this code works but I want it to work for the functions given by the user.Please explain me how can I call the user-defined cost function.

採用された回答

Nirav Sharda
Nirav Sharda 2017 年 2 月 22 日
It looks like the function PSOcodeRTDA needs a function handle as the first argument but because it is getting a char vector its not working. Try adding this line after the handles.name = char(name) line.
handles.functionHandle = str2func(name);
Then use the functionHandle in the other functions callback instead of name. The str2func creates function handle from character vector. I hope this helps!

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by