필터 지우기
필터 지우기

GUI: How do save data from one callback to another ?

조회 수: 3 (최근 30일)
polo Mahmoud
polo Mahmoud 2019년 10월 23일
댓글: polo Mahmoud 2019년 10월 24일
Hi i want to save my data from one callback and use that in other callback.
eg. i have 2 button, one to run the first callback and the other to run the second callback.
eg.
function a_Callback(hObject, eventdata, handles)
a = 2+2;
function c_Callback(hObject, eventdata, handles)
c = 2+a;
how do i save or use the " a=2+2 " from the first one to be saved to the next callback, so if i click on "button 2" it will give me eg. 6.

채택된 답변

Guillaume
Guillaume 2019년 10월 23일
The handles structure that your callback receives is meant exactly for this:
%It's a good idea to create the variable used in the callback when the gui is first created. e.g. in the OpeningFcn callback
function mygui_OpeningFcn(hObject, eventdata, handles)
handles.a = NaN;
%...
end
function a_Callback(hObject, eventdata, handles)
handles.a = 2+2;
guidata(hObject, handles) %save new state of handles
end
function c_Callback(hObject, eventdata, handles)
c = 2 + handles.a;
%...
end
  댓글 수: 3
Guillaume
Guillaume 2019년 10월 23일
You can initialise it to whatever you want, but it's a good idea to initialise to something when the GUI is created. One option is to do that in the OpeningFcn.
If you don't initialise it and the user click on the 'c' button without having clicked once on the 'a' button, then you'll get an error in the 'c' callback since the variable wouldn't exist.
polo Mahmoud
polo Mahmoud 2019년 10월 24일
okay thank you very much :)

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by