How to call user defined cost function through MATLAB GUI?
2 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
采纳的回答
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Migrate GUIDE Apps 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!