passing variables between .m files using GUI
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ahmad
am 30 Okt. 2015
Beantwortet: Geoff Hayes
am 30 Okt. 2015
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 .
0 Kommentare
Akzeptierte Antwort
Geoff Hayes
am 30 Okt. 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.
0 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Migrate GUIDE Apps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!