필터 지우기
필터 지우기

Passing an image between GUIDE GUI's

조회 수: 1 (최근 30일)
Jason
Jason 2018년 2월 6일
댓글: Jason 2019년 3월 13일
Hi, I am trying to pass an image between GUIDE GUI's and have read the various articles - and thought it was straight forward.
I prefer the setappdata / getappdata approach.
My two GUIS have had their "Tag" properties renamed GUI1 & GUI2.
This is my code taking an image from an axes component from GUI1 and trying to display it on an axes component on GUI2.
In a pushbutton call back on GUI 1:
IM=double(getimage(handles.axes1))
setappdata(handles.GUI1,'IM',IM)
GUI2 % This invokes GUI2
And then in the openingFCN on GUI2
IM=getappdata(handles.GUI2,'IM')
axes(handles.axes1)
imshow(IM)
But the IM variable is empty
IM =
[]
  댓글 수: 2
Jason
Jason 2018년 2월 6일
편집: Jason 2018년 2월 6일
OK, I have it working - I needed to change to:
setappdata(0,'IM',IM)
getappdata(0,'IM')
I've also decided to pass a struct so I can pass many things but with one object. I pass the image via:
IM=double(getimage(handles.axes1));
s.IM=IM
setappdata(0,'myStruct',s)
Jan
Jan 2018년 2월 6일
Storing data in the ApplicationData of the root object has the same disadvantages as other methods to create global variables. This is not a smart idea for sharing data.

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

답변 (1개)

Jan
Jan 2018년 2월 6일
편집: Jan 2019년 2월 14일
Do not use global variables to share data. You find thousands of threads concerning Matlab or other programming languages, which explain the severe problems. A direct sharing is much better:
% File: GUI1.m -----------------------------------------
function GUI1
handles.FigH = figure('Tag', 'GUI1');
handles.AxesH = axes;
guidata(handles.FigH, handles);
end
% File: GUI2.m -----------------------------------------
function GUI2
handles.FigH = figure('Tag', 'GUI2');
handles.ButtonH = uicontrol('Style', 'PushButton', 'Callback', @ButtonCB);
guidata(handles.FigH, handles);
end
function ButtonCB(ButtonH, EventData)
handlesGUI2 = guidata(ButtonH); % Not needed here
data = rand(100, 100);
GUI1H = findobj(allchild(groot), 'flat', 'Tag', 'GUI1');
handlesGUI1 = guidata(GUI1H);
image(data, 'Parent', handlesGUI1.AxesH);
end
Now the pressing the button in the 2nd GUI searches GUI1 at first and creates an image in its axes. You can do everything you want from inside the GUI2 callback, if you get the handles of the GUI1 at first.
It is more efficient to search "GUI1" once only during the creation of GUI2:
function GUI2
handles.FigH = figure('Tag', 'GUI2');
handles.ButtonH = uicontrol('Style', 'PushButton', 'Callback', @ButtonCB);
handles.GUI1H = findobj(allchild(groot), 'flat', 'Tag', 'GUI1');
guidata(handles.FigH, handles);
end
function ButtonCB(ButtonH, EventData)
handlesGUI2 = guidata(ButtonH); % Not needed here
data = rand(100, 100);
GUI1H = handles.GUI1H;
...
This works also if you use GUIDE. Then the 3rd argument handles is provided as input already and you do not have to obtain it by guidata.
  댓글 수: 5
Jan
Jan 2019년 3월 13일
@Shoaib Bilal: The code works with programmatically created GUIs as well as with GUIDE. Remember, that GUIDE creates a Figure and the code for the callbacks, while a programmatic approach creates the Figure dynamically - this is reall equivalent. Even if the functions are created by GUIDE, you can insert the suggested code.
"because its not working for my gui" - Then there is a programming error in your code. Please open a new thread and post your code together with a description of the problem. Then the forum will help you to fix the error.
Jason
Jason 2019년 3월 13일
Hi, I pass a struct to the 2nd GUI (in my example I pass the image, plus the contents of text object
Image=double(getimage(handles.axes4));
s.IM=Image;
s.path=get(handles.textSaveFolder,'String');
guidata(hObject,handles); % now we save the data
LTF(s) %pass struct to GUI2 , can also pass image directly
and then in the 2nd GUI openingFCN, I do:
s=varargin{1};
handles.myStruct=s;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes LTF wait for user response (see UIRESUME)
% uiwait(handles.LTF);
Image=s.IM;

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

카테고리

Help CenterFile Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by