passing variables between .m files using GUI

4 ビュー (過去 30 日間)
Ahmad
Ahmad 2015 年 10 月 30 日
回答済み: Geoff Hayes 2015 年 10 月 30 日
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 .

採用された回答

Geoff Hayes
Geoff Hayes 2015 年 10 月 30 日
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 件)

カテゴリ

Help Center および File ExchangeMigrate GUIDE Apps についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by