How to call user defined cost function through MATLAB GUI?

2 vues (au cours des 30 derniers jours)
Shrivardhan Suryawanshi
Shrivardhan Suryawanshi le 19 Fév 2017
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.

Réponse acceptée

Nirav Sharda
Nirav Sharda le 22 Fév 2017
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!

Plus de réponses (0)

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by