how to use a (matrix) variable from one callback to another callback in GUI?

조회 수: 1 (최근 30일)
I load a cell from a .mat file and convert it to mat and save it in a variable called "MelisaW". Now, I want to use this MelisaW in another call back function lets say PopUpMenu. So How do I use it?
function StartAnimation_Callback(hObject, eventdata, handles)
% hObject handle to StartAnimation (see GCBO)
load('PMelisa01.mat', 'PMelisaD20180404Walkingnone01');
MelisaW = cell2mat(PMelisaD20180404Walkingnone01);
Plot(MelisaW(1,1),MelisaW(2,2));
End
Another callback: want to access "MelisaW" in the cases of switch-case.
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
ax2 = handles.axes2;
str = get(hObject, 'String');
val = get(hObject,'Value');
switch str{val};
case'head'
for i=1:200
PMelisaD20180404Head_Px=MelisaW(i,1)/100; %here i want to use that MelisaW
plot(ax2,i,PMelisaD20180404Head_Px,'o');
end
  댓글 수: 6
Ameer Hamza
Ameer Hamza 2018년 4월 24일

I agree with you that I should have provided a reference to the resources supporting the statement, which I have added now. But still, there is one confusion. Although you added the loaded matrix to the handle in your first callback, what will happen when the first callback is executed again and the following line is executed

S = load('PMelisa01.mat', 'PMelisaD20180404Walkingnone01');

It appears that MATLAB will again load the file, or is there something going on.

It makes sense that for custom GUI, there is no need to load variable in the base workspace. But instead of loading it in a repeating callback function, it might be better to load it in createFcn of the graphics object and assign add it to the handle. That way, repeated calls to load() can be avoided.

Stephen23
Stephen23 2018년 4월 24일

"It appears that MATLAB will again load the file..."

This is correct: the file will get loaded every time that callback runs. This is simply because of how that GUI has been designed by its author: it may be that that is exactly what the author intended... or maybe not. I have no idea, because the design requirements were not given in the question. Perhaps the data file is changed by an external program, and the GUI should load it again each time. I have no idea, because we were not told anything about this.

As you point out there is no need for the data to be reloaded each time: it would be quite easy to provide a button that lets the user load the data whenever they wanted to, or to add some conditions that check if the data has already been loaded (e.g. check if handles.data exists, or the size, or with a flag), or to load the data once automatically at the start... however these are design considerations for the GUI's author: things to be decided based on what it needs to do and how it should interact with the user and data. It is not my job to decide that.

"it might be better to load it in createFcn of the graphics object and assign add it to the handle"

Personally I am not a big fan of things happening automatically like that (particularly when things go wrong, as they inevitably will...), but that would certainly work.

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

채택된 답변

Stephen23
Stephen23 2018년 4월 24일
편집: Stephen23 2018년 4월 24일
There are several ways to pass data between callbacks:
I personally like using nested functions, as they are very simple and intuitive, but sadly cannot be used with GUIDE. With GUIDE the easiest is to use guidata, for which you will need to:
  • callback 1: add the data as a field of the handles structure.
  • callback 1: at the very end of the callback save handles using guidata.
  • callback 2: the handles structure contains your data as field.
Something like this:
function StartAnimation_Callback(hObject, eventdata, handles)
% hObject handle to StartAnimation (see GCBO)
S = load('PMelisa01.mat', 'PMelisaD20180404Walkingnone01');
handles.MW = cell2mat(struct2cell(S));
plot(handles.MW(1,1),handles.MW(2,2));
guidata(hObject,handles)
End
and the in the other callback:
function popupmenu1_Callback(hObject, eventdata, handles)
% hObject handle to popupmenu1 (see GCBO)
ax2 = handles.axes2;
Melissa = handles.MW;
...
end

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by