passing variables between .m files using GUI

1 view (last 30 days)
Hi there, i have a gui that contain some edit text and 2 push button. i used one push button(start) to execute an .m file (start.m) and fetch the value of edit text(epoch) and use it in some mathematical equations. for example :
epochs= str2double(get(handles.epoch,'String'));
y=epoch*2;
And i used another push button (test) to execute another .m file (test.m) .i want to use variable (y) in this new .m file but i have error (Undefined function or variable 'y'.).
I know it's a logical error, but i'm asking for a simple solution.
i tried make the .m files as functions. but also it gives me this error using first .m file (Undefined variable "handles" or class "handles.epoch".).
PLEASE i have to do my project in 2 days .

Accepted Answer

Geoff Hayes
Geoff Hayes on 30 Oct 2015
Ahmad - you can easily make data available in other callbacks by using the handles structure (which is passed as the third parameter to each of your callbacks, assuming that you are using GUIDE). What you need to do is save the y data to handles in the callback for your pushbutton start, and then access it from handles in the callback for your pushbutton stop. For example,
function start_Callback(hObject, eventdata, handles)
epochs= str2double(get(handles.epoch,'String'));
y=epoch*2;
handles.y = y;
guidata(hObject,handles); % save the updated handles structure
Note the use of guidata which is important in saving the updated handles structure. Now, in your second callback access y as
function stop_Callback(hObject, eventdata, handles)
if isfield(handles,'y')
y = handles.y;
% call your function that you wish to pass t to
end
And that is it. The isfield is used above to make sure that the y exists in handles before we try to access it.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!