Retrieve data from a guidata from another file

I have a counter that increment each time i click on a pushbutton and the value is stored in a guidata which works it gives me the wanted value. (The function that increments is in the same file as the gui)
But i want to get the value form the guidata in another file as a condifition for a if but i always return me this error :
Undefined variable "handles" or class "handles.X".
Error in kgexec4n (line 67)
if get(guidata(hf,handles.X),'Value')==1
Error while evaluating uicontrol Callback
and here is the gui code with the function and the pushbutton
function h0=kgui4n
%KGUI4N construction de la boite de dialogue pour KGEXEC4N
%CAVIAR2, © ALSTOM + OL 1999/12-2003/03
blanc=[1 1 1];
gris=blanc*0.75;
%figure
h0=figure(...
'Units','characters',...
'DefaultUicontrolUnits','characters',...
'Color',gris,...
'DefaultUicontrolBackgroundColor',gris,...
'HandleVisibility','callback',...
'IntegerHandle','off',...
'MenuBar','none',...
'Name',ch00{lg},...
'NumberTitle','off',...
'Position',[100 30 110 25],...
'Resize','on',...
'Tag','NormeDlg',...
'WindowStyle','modal',...
'ToolBar','none');
%% Pushbutton
handles.X = 0;
handles.button = uicontrol(h0,...
'Callback',@buttonCB,...
'Position',[10 0.5 9 2],...
'String','count',...
'Tag','count');
guidata(h0, handles);
%%%%%%%%%%%%%
function buttonCB(ButtonH, EventData)
handles = guidata(ButtonH);
handles.X = handles.X + 1;
if handles.X > 3
handles.X = 0
end
guidata(ButtonH, handles)
I would like to know how i could get the value of handles.X which is stored in the guidata and use it in another file ?

 Accepted Answer

handles_hf = guidata(hf);
if handles_hf.X.Value == 1

6 Comments

It doesn't work but this time because when i write
handles_hf = guidata(hf);
it says that hf is undefined as a function or variable or it is set in the beginning of the function as
hf = kgui4n %%%kgui4n is the gui
nevermind it still gives me the same error as the begining
Attempt to reference field of non-structure array.
Error in kgexec4n (line 94)
if handles_hf.X.Value == 1
Error while evaluating uicontrol Callback
i don't really understand why as i my hf is well defined could it be that because in the function that increment the counter i use guidata(ButtonH,handles) and not h0 as h0=figure?
Your handles.X is just a numeric variable, not a graphics object that you can get() a property of.
hf = kgui4n;
handles_hf = guidata(hf);
if handles_hf.X == 1
disp('X is true')
else
disp('X is false')
end
X is false
function h0=kgui4n
%KGUI4N construction de la boite de dialogue pour KGEXEC4N
%CAVIAR2, © ALSTOM + OL 1999/12-2003/03
blanc=[1 1 1];
gris=blanc*0.75;
lg = 1; ch00{lg} = 'test gui';
%figure
h0=figure(...
'Units','characters',...
'DefaultUicontrolUnits','characters',...
'Color',gris,...
'DefaultUicontrolBackgroundColor',gris,...
'HandleVisibility','callback',...
'IntegerHandle','off',...
'MenuBar','none',...
'Name',ch00{lg},...
'NumberTitle','off',...
'Position',[100 30 110 25],...
'Resize','on',...
'Tag','NormeDlg',...
'WindowStyle','modal',...
'ToolBar','none');
%% Pushbutton
handles.X = 0;
handles.button = uicontrol(h0,...
'Callback',@buttonCB,...
'Position',[10 0.5 9 2],...
'String','count',...
'Tag','count');
guidata(h0, handles);
end
%%%%%%%%%%%%%
function buttonCB(ButtonH, EventData)
handles = guidata(ButtonH);
handles.X = handles.X + 1;
if handles.X > 3
handles.X = 0
end
guidata(ButtonH, handles)
end
Your answer works when i try to get the data from the guidate in the file where my gui is which is kgui4n.
But what i am trying to do is get the data i am using the guidata hf in another file, there might have been an misunderstanding on that part. When i try to use the guidata in another file it doesn't work and doesn't recognize hf even if i associate it with the gui.
You need some way of getting the handle of the figure created by kgui4n . For example you might do
hf = findobj(groot, 'type', 'figure', 'tag', 'NormeDlg');
handles_hf = guidata(hf);
if handles_hf.X == 1
etc
end
thank you it made the trick

Sign in to comment.

More Answers (0)

Categories

Find more on Interactive Control and Callbacks in Help Center and File Exchange

Products

Release

R2012a

Asked:

Ali
on 30 Sep 2022

Commented:

Ali
on 30 Sep 2022

Community Treasure Hunt

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

Start Hunting!