フィルターのクリア

Problem in function handling using callback functions GUI

2 ビュー (過去 30 日間)
Subhiksha
Subhiksha 2013 年 8 月 30 日
コメント済み: guada 2013 年 12 月 21 日
I have a user defined function named myfunction(). I have designed a layout using guide.The layout(guimain.m) contains a edit box and a push button. The user enters an input in the edit box and clicks the push button. On clicking the push button myfunction should be called from push button_callback. myfunction.m is in a folder called functions inside the folder that contains the guimain.m
This works fine if the function is first executed separately, and then guimain.m is executed. But this is not feasible as we are developing a tool.
*My sample guimain.m looks like this*
function load_Callback(hObject, eventdata, handles)
% hObject handle to load (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Retrieving input from the text box using get()
var=get(handles.edit1,'string');
myfunction(var)
*myfunction.m looks like:*
function myfunction(a)
sum=var+var;
disp(sum)
end
Can anyone please help me in this regard. I am a newbie in GUI.
Thanks in advance for the help

採用された回答

Walter Roberson
Walter Roberson 2013 年 8 月 30 日
Why are you referring to "var" inside myfunction(), when the value you need has been passed as as "a" ?
  1 件のコメント
Subhiksha
Subhiksha 2013 年 8 月 30 日
sorry but that doesn't solve my problem.. It should be called as myfunction(var)

サインインしてコメントする。

その他の回答 (2 件)

David Sanchez
David Sanchez 2013 年 8 月 30 日
You say your GUI has an edit box and a push-button, you should have a callback function for each object in your GUI, then, the call to myfunction should be within the callback function of your push-button. Actually, to avoid having a call to another function, the code in the callback function of your push button should look something like this:
var=get(handles.edit1,'string');
sum=var+var;
disp(sum)
end
Make sure that the objects are properly linked to their callbacks, check the property inspector for that.
  1 件のコメント
Subhiksha
Subhiksha 2013 年 8 月 30 日
Thanks much for the help.
Yes, call to myfunction is within the callback function of the push-button.
It works fine if I execute myfunction first and then the guimain.m. The problem comes when I execute the _guimain.m _first without executing the external functions.
But since I am developing this as a tool each time it is not possible to execute all the external functions and then execute the main function.
So kindly help me in this regard.
Thanks in advance

サインインしてコメントする。


ES
ES 2013 年 8 月 30 日
Hello, there are three ways to handle this. 1. Place your logic of my_function() inside the Button Call back function as David Sanchez has said above.
2. Otherwise cut and paste my function in your guimain.m file. I do this all the time. So you will have a single file guimain.m, with the GUI related stuff at the top and your logic(my_function()) at the bottom.
3. or if you wan t to abstract logic from GUI, then try adding the path of the my_function file in your gui code (eg path(path,path of myFunction.m)
Personally, option 2 will be my choice.
  4 件のコメント
ES
ES 2013 年 8 月 30 日
guimain.m
function pushbutton1_Callback(hObject, eventdata, handles)
Value_1 = get(handles.listbox1, 'Value');%%get Value
my_function(value5);%%Call my_function
my_function.m
function my_function(x)
disp(x);
end
guada
guada 2013 年 12 月 21 日
guimain.m
function pushbutton1_Callback(hObject, eventdata, handles)
Value_1 = get(handles.listbox1, 'Value');%%get Value
my_function(value5);%%Call my_function
my_function.m
function my_function(x)
disp(x);
end
how can i display the answer at the static txt of the gui?

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeInteractive Control and Callbacks についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by