Hi everyone,
I want to open a GUI (GUI_Retrait) in my first GUI (GUI3) but I have trouble with it. I use GUIDE for GUI3 and I create GUI_Retrait without GUIDE.
To call GUI_Retrait I use the method described here : http://fr.mathworks.com/matlabcentral/answers/248830#answer_196171
I have an error in my first GUI when I'm calling the second one. The error is :
Error using getappdata
Value must be a handle.
Error in GUI3>Retrait_Callback (line 279)
handles.GUI_Retrait_struct=getappdata(handles.GUI_Retrait); % save the handle of
gui_b in gui_a
Error in gui_mainfcn (line 96)
feval(varargin{:});
Error in GUI3 (line 42)
gui_mainfcn(gui_State, varargin{:});
Error in
@(hObject,eventdata)GUI3('Retrait_Callback',hObject,eventdata,guidata(hObject))
Error while evaluating uicontrol Callback
I think I don't create my second GUI (GUI_retrait) the right way. I don't know how to create the handle of the GUI itself.
Does someone know how to fix this?
Here is the code of my second GUI : (I have several checkboxes which depend on a number given by GUI3)
function handles=GUI_Retrait
%Retrive Datas from GUI3
handles.GUI3=getappdata(0,'GUI3'); % retrieve handle of GUI_A
handles.GUI3_struct=getappdata(handles.GUI3); % retrieve handle to the structure used in GUI_A
handles.GUI3_data=handles.GUI3_struct.UsedByGUIData_m; % retrieve data inside the structure
%Figure
handles.GUI_Retrait.h=figure('Name','Eprouvette(s) à retirer','NumberTitle','off','Position',[300 300 300 400]);
%PushButton
handles.GUI_Retrait.pb=uicontrol('Parent',handles.GUI_Retrait.h,'Style', 'Pushbutton',...
'String', {'Retirer'},...
'Position', [150 100 120 50],...
'Callback',@pb_Callback);
%Checkboxes
for i=1:handles.GUI3_data.n
str=sprintf('Eprouvette n°%s',num2str(i));
handles.GUI_Retrait.box(i)=uicontrol('Parent',handles.GUI_Retrait.h,...
'Style', 'checkbox',...
'String', {str},...
'Position', [20 400-50*i 100 50]);
end
guidata(handles.GUI_Retrait.h,handles);
Thanks. Clément

2 commentaires

Adam
Adam le 30 Mar 2016
getappdata(handles.GUI_Retrait)
assumes that handles.GUI_Retrait is a graphics object that you have previously used setappdata to set some data on.
It is difficult to assess from the code you have posted because it doesn't seem to include the line that yields the error, but assuming your 'handles' here is the same one as that returned by your GUI_Retrait function then
handles.GUI_Retrait
is a structure, not a graphics object handle because you are just creating it with fields in a line such as:
handles.GUI_Retrait.h=figure('Name','Eprouvette(s) à retirer','NumberTitle','off','Position',[300 300 300 400]);
I'm not too familiar with doing a programmatic UI in a function because I always do mine in a class which naturally stores all the components I need so it doesn't require the concept of appdata so it's hard to give any concrete advice on small changes to fix your problem, especially since I suspect some of the problems originate from code you haven't included.
Clément P
Clément P le 31 Mar 2016
Modifié(e) : Clément P le 31 Mar 2016
Hi Adam,
Thanks for your answer.
It is right that I didn't created the handles first. Now I have added the line :
setappdata(0,'GUI_Retrait',gcf);
But I still have the same error. I assume GUIDE is doing something that I'm not doing right manually in GUI_Retrait. I also tried the command "handles=guihandles" but I still have the same error.

Connectez-vous pour commenter.

 Réponse acceptée

Orion
Orion le 30 Mar 2016

0 votes

Hi,
When you call your second gui (GUI_Retrait), do you first close the main gui ?
If so, the error message is normal.
For what I understand, when you create the main GUI, you stock the handle in the appdata of the root (setappdata(0,'GUI3',yourHandle)). So the handle is stored. But if you close the main figure, the value of the handle is still stored, but the GUI doesn't exist anymore, so you can't use the value as a handle.
to be clear, test this :
f = figure;
ishandle(f) % the answer is 1
close(f); % destroy the object
ishandle(f) % the answer is 0
f still exist but is not a handle anymore.
Is this the problem you have ?

9 commentaires

Clément P
Clément P le 31 Mar 2016
Hi Orion,
Thanks for answering.
I do not close my main GUI when I open the second. I don't have issue when I open the second GUI. The problem occurs when I want to retrieve the handles of my second GUI (GUI_Retrait) in the main (GUI3).
I expect the problem to be in the code of my second GUI, because I'm not familiar with programming GUI manually.
Orion
Orion le 31 Mar 2016
I just saw Adam's comment, and I guess he's right.
You indicate that the line that generates an error is :
handles.GUI_Retrait_struct=getappdata(handles.GUI_Retrait);
Or handles.GUI_Retrait is not the handle of your GUI. It's just the structure you created in your script.
You need to change this line by :
handles.GUI_Retrait_struct=getappdata(handles.GUI_Retrait.h); % the handle is stored in the subfield h.
Clément P
Clément P le 31 Mar 2016
Yes this is what I want to do, but no only for the handle of h. I want to have the handles of the boxes and the pushbutton.
That is why I created a structure of handles name "GUI_Retrait" with the line :
setappdata(0,'GUI_Retrait',gcf);
But it seems to be only a structure and not a handle. And so I can't get it with getappdata.
Orion
Orion le 31 Mar 2016
Actually, the "weird" part of your code is that you are mixing the data of the two gui in only one structure handles. In my opinion, this is not a good way to work and will generate some other problems.
What you should probably do is something like that
function handles=GUI_Retrait
% Retrive Datas from GUI3
handlesGUI3 = guidata(getappdata(0,'GUI3')); % retrieve handle of GUI_A
%%Use the variable handles for your local GUI.
%Figure
handles.h=figure('Name','Eprouvette(s) à retirer','NumberTitle','off','Position',[300 300 300 400]);
%PushButton
handles.pb=uicontrol('Parent',handles.h,'Style', 'Pushbutton',...
'String', {'Retirer'},...
'Position', [150 100 120 50],...
'Callback',@pb_Callback,...
'Tag','GUI_RETRAIT');
%Checkboxes
for i=1:handlesGUI3.n
str=sprintf('Eprouvette n°%s',num2str(i));
handles.box(i)=uicontrol('Parent',handles.h,...
'Style', 'checkbox',...
'String', {str},...
'Position', [20 400-50*i 100 50]);
end
guidata(handles.h,handles);
Note that I added the use of the Tag property to create the figure.
Once this is done, you can retrieve all your data (handles of the checkboxes for examples) easily :
% get the handle of the figure with the wanted tag
myfig = findall(0,'Tag','GUI_RETRAIT');
% get all the data stored
>> h = guidata(myfig )
h =
h: [1x1 Figure]
pb: [1x1 UIControl]
box: [1x4 UIControl]
Clément P
Clément P le 31 Mar 2016
Thank you very much Orion.
It is really better this way and working perfectly.
Last question, if I want to retrieve the handles of GUI3 in my pb_callback function (so in GUI_Retrait), do I use the getappdata the same way? Or is there a way have it directly in my workspace when using the callback function. (Just to avoid loading twice the datas in the same GUI).
Orion
Orion le 31 Mar 2016
you can use the data of GUI3 as an argument of your callback function.
replace
'Callback',@pb_Callback,...
by
'Callback',{@pb_Callback,handlesGUI3},...
then change the prototype of the callback function.
function pb_Callback(hObject, eventdata)
to
function pb_Callback(hObject, eventdata,myHandlesStruct)
An other way to work is using the getappdata as I showed it :
function pb_Callback(hObject, eventdata)
myHandlesStruct = guidata(findall(0,'Tag','GUI3');)
Assuming you put the Tag GUI3 on your main GUI.
Clément P
Clément P le 31 Mar 2016
It's funny that you only add the Tag 'GUI_Retrait' on the PushButton but it gives me the handles of the figure and checkboxes too.
Anyway it works perfectly now.
Thanks a lot Orion ! ;)
Orion
Orion le 31 Mar 2016
My bad ! :)
I wanted to put this tag on the figure.
And you should put this tag on the figure.
It works because of the guidata function which search automatically the figure parent of the handle. That's just good luck :)
Clément P
Clément P le 31 Mar 2016
Ahah ! First time I'm lucky with Matlab :D
Thanks again !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Interactive Control and Callbacks dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by