How to call user defined cost function through MATLAB GUI?

2 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Nirav Sharda
Nirav Sharda am 22 Feb. 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!

Weitere Antworten (0)

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by