Reputation: 45
I have some functions create on matlab (.m). And I would like to call them on an interface GUI : how my push button callback function can call a function .m (which is in the same workspace) ? Moreover, my function returns some variables so I would like to keep these variables in my workspace in order to access them from other buttons of my interface. And after, is it possible to put the result of a variable on my interface ?
Thank you in advance,
Best regards
Upvotes: 0
Views: 2605
Reputation: 13945
Yes it's possible and is quite simple.
You can call your .m file directly in the pushbutton callback and fetch the results as you would in any other script.
Let's consider a simple example in which you call function A
from the callback. Let's say function A
outputs 2 arguments, out1
and out2
.
In the .m file of function A, the function is defined as follows (input arguments can be anything of course):
function [ou1,out2] = A(Input arguments)
%// code here
end
Then in the pushbutton callback in your GUI, use this syntax to retrieve the outputs of function A
and use them:
[B,C] = A(Input arguments); %// Or out1 and out2, the names don't matter here.
Better yet, to share those data with other callbacks from your GUI, you can store the variables in what is called guidata
, or the data associated with the GUI. There is a convenient handles structure used to store any kind of data and easily share them between callbacks.
Therefore, in your case you would use:
[handles.B, handles.C] = A(Input arguments)
and as such handles.B
and handles.C
are accessible from any callback in your GUI. Don't forget to update the handles structure at the end of the callback with this command:
guidata(hObject,handles)
where hObject
is the handles to the GUI's figure. For more info about that check the docs here.
Upvotes: 1