passing variables between .m files using GUI

4 visualizzazioni (ultimi 30 giorni)
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 .

Risposta accettata

Geoff Hayes
Geoff Hayes il 30 Ott 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.

Più risposte (0)

Categorie

Scopri di più su Migrate GUIDE Apps in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by