필터 지우기
필터 지우기

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 CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by