Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
saving handles

Subject: saving handles

From: Roland

Date: 22 Jul, 2009 08:33:03

Message: 1 of 9

hi!

still i dont understand, how do deal correctly with handles...

my code should do the following:

create a pop up menu, while the menu is created: draw 2 rectangles and save their handles.

this is the code:

function DrawRectangles

H.hFig = figure('units', 'normalized',...
                'pos', [.1 .1 .8 .8]);
H.hAxis = axes( 'Position', [0 0 1 1],...
                'color', 0.8*[1 1 1],...
                'XTick', [],...
                'YTick', []);
axis([0 30 0 30])

guidata(H.hFig, H) % save app data

H.hnRecsPop=uicontrol('style','popupmenu',...
                       'string','2',...
                       'units','normalized',...
                       'position',[.07 .9 .04 0.04],...
                       'CreateFcn',@nRecsPop_Callback);
             
guidata(H.hFig, H)
save H

function nRecsPop_Callback(hObject,varargin)
H = guidata(gcbf); %load app data

H.nRecs = get(hObject,'value')+1; %number of rectangles to draw

%%% draw rectangles %%%
alpha=-pi/2:2*pi/H.nRecs:3*pi/2;
width=5;
height=5;
H.xPos=15-12*cos(alpha); H.yPos=15+12*sin(alpha); %Position of Rectangles
for i=1:H.nRecs
    %draw rectangle
    H.hRec(i)=rectangle('Position',[H.xPos(i)-width/2 H.yPos(i)-height/2 width height],...
                              'FaceColor',[1 1 1]);
end
    
guidata(H.hFig, H) %save app data

now when i load my H variable, i think it should contain H.hRec, which is created at the uicontrol creation callback from the pop up menu and stored via
guidata(H.hFig, H)

but it does not contain H.hRec. why?

Subject: saving handles

From: us

Date: 22 Jul, 2009 08:44:04

Message: 2 of 9

"Roland " <burgmann@gmx.de> wrote in message <h46irv$dd7$1@fred.mathworks.com>...
> hi!
>
> still i dont understand, how do deal correctly with handles...
>
> my code should do the following:
>
> create a pop up menu, while the menu is created: draw 2 rectangles and save their handles.
>
> this is the code:
>
> function DrawRectangles
>
> H.hFig = figure('units', 'normalized',...
> 'pos', [.1 .1 .8 .8]);
> H.hAxis = axes( 'Position', [0 0 1 1],...
> 'color', 0.8*[1 1 1],...
> 'XTick', [],...
> 'YTick', []);
> axis([0 30 0 30])
>
> guidata(H.hFig, H) % save app data
>
> H.hnRecsPop=uicontrol('style','popupmenu',...
> 'string','2',...
> 'units','normalized',...
> 'position',[.07 .9 .04 0.04],...
> 'CreateFcn',@nRecsPop_Callback);
>
> guidata(H.hFig, H)
> save H
>
> function nRecsPop_Callback(hObject,varargin)
> H = guidata(gcbf); %load app data
>
> H.nRecs = get(hObject,'value')+1; %number of rectangles to draw
>
> %%% draw rectangles %%%
> alpha=-pi/2:2*pi/H.nRecs:3*pi/2;
> width=5;
> height=5;
> H.xPos=15-12*cos(alpha); H.yPos=15+12*sin(alpha); %Position of Rectangles
> for i=1:H.nRecs
> %draw rectangle
> H.hRec(i)=rectangle('Position',[H.xPos(i)-width/2 H.yPos(i)-height/2 width height],...
> 'FaceColor',[1 1 1]);
> end
>
> guidata(H.hFig, H) %save app data
>
> now when i load my H variable, i think it should contain H.hRec, which is created at the uicontrol creation callback from the pop up menu and stored via
> guidata(H.hFig, H)
>
> but it does not contain H.hRec. why?

well,...
the short answer: it's simply not SAVEd...
the longer answer: you SAVE your handles before you create H.hRec in your callback function...
the note: it's never a good idea to SAVE/LOAD handles to/from a file as they may become invalid during runtime...

us

Subject: saving handles

From: Roland

Date: 22 Jul, 2009 09:03:03

Message: 3 of 9


> well,...
> the short answer: it's simply not SAVEd...
> the longer answer: you SAVE your handles before you create H.hRec in your callback function...
> the note: it's never a good idea to SAVE/LOAD handles to/from a file as they may become invalid during runtime...
>
> us

well, thats not true. the the variable H is saved in the end and the callback function is called during the creation of the pop up menu.

i only us the save function to demonstrate, that H.hRec is not stored. i could also set a breakpoint and check, what is stored in H, and the result is the same.

Subject: saving handles

From: us

Date: 22 Jul, 2009 10:55:36

Message: 4 of 9

"Roland " <burgmann@gmx.de> wrote in message <h46kk6$3a1$1@fred.mathworks.com>...
>
> > well,...
> > the short answer: it's simply not SAVEd...
> > the longer answer: you SAVE your handles before you create H.hRec in your callback function...
> > the note: it's never a good idea to SAVE/LOAD handles to/from a file as they may become invalid during runtime...
> >
> > us
>
> well, thats not true. the the variable H is saved in the end and the callback function is called during the creation of the pop up menu.
>
> i only us the save function to demonstrate, that H.hRec is not stored. i could also set a breakpoint and check, what is stored in H, and the result is the same.

again, not quite correct...
let's look at the flow

1) create fig and axis -> save H (guidata_1)
2) create uicontrols -> H.hRec correctly is in H -> save H (guidata_2 in callback)
3) the latest H is NOT retrieved, eg, using H=guidata(H.hFig), AFTER the
   controls have been created...
4) rather, you simply save your old H (guidata_1) AGAIN in guidata_3

us

Subject: saving handles

From: Roland

Date: 22 Jul, 2009 18:22:03

Message: 5 of 9

"us " <us@neurol.unizh.ch> wrote in message <h46r78$g7e$1@fred.mathworks.com>...
> "Roland " <burgmann@gmx.de> wrote in message <h46kk6$3a1$1@fred.mathworks.com>...
> >
> > > well,...
> > > the short answer: it's simply not SAVEd...
> > > the longer answer: you SAVE your handles before you create H.hRec in your callback function...
> > > the note: it's never a good idea to SAVE/LOAD handles to/from a file as they may become invalid during runtime...
> > >
> > > us
> >
> > well, thats not true. the the variable H is saved in the end and the callback function is called during the creation of the pop up menu.
> >
> > i only us the save function to demonstrate, that H.hRec is not stored. i could also set a breakpoint and check, what is stored in H, and the result is the same.
>
> again, not quite correct...
> let's look at the flow
>
> 1) create fig and axis -> save H (guidata_1)
> 2) create uicontrols -> H.hRec correctly is in H -> save H (guidata_2 in callback)
> 3) the latest H is NOT retrieved, eg, using H=guidata(H.hFig), AFTER the
> controls have been created...
> 4) rather, you simply save your old H (guidata_1) AGAIN in guidata_3
>
> us

okay, thank you so far for your help! now i think i understand the problem. with the 3rd guidata, you call it guidata_3, i overwrite H.hRec.

now i modified my code a bit. i use now display(H) to display my handles insted of the save function. And insted of the third guidata(H.hFig,H) i use H=guidata(H.hFig)

now the code looks like this:

function DrawRectangles

H.hFig = figure('units', 'normalized',...
                'pos', [.1 .1 .8 .8]);
H.hAxis = axes( 'Position', [0 0 1 1],...
                'color', 0.8*[1 1 1],...
                'XTick', [],...
                'YTick', []);
axis([0 30 0 30])

guidata(H.hFig, H) % save app data

H.hnRecsPop=uicontrol('style','popupmenu',...
                       'string','2',...
                       'units','normalized',...
                       'position',[.07 .9 .04 0.04],...
                       'CreateFcn',@nRecsPop_Callback);
             
H=guidata(H.hFig);
display(H)

function nRecsPop_Callback(hObject,varargin)
H = guidata(gcbf); %load app data

H.nRecs = get(hObject,'value')+1; %number of rectangles to draw

%%% draw rectangles %%%
alpha=-pi/2:2*pi/H.nRecs:3*pi/2;
width=5;
height=5;
H.xPos=15-12*cos(alpha); H.yPos=15+12*sin(alpha); %Position of Rectangles
for i=1:H.nRecs
    %draw rectangle
    H.hRec(i)=rectangle('Position',[H.xPos(i)-width/2 H.yPos(i)-height/2 width height],...
                              'FaceColor',[1 1 1]);
end
    
guidata(H.hFig, H) %save app data

----End Code---

okay, now H.hRec is stored in H, but now H.hnRecsPop is not stored. how can i get this fixed? how can i store both, H.hRec and H.hnRecsPop?

Subject: saving handles

From: us

Date: 22 Jul, 2009 18:35:19

Message: 6 of 9

"Roland "
> function nRecsPop_Callback(hObject,varargin)
> H = guidata(gcbf); %load app data
> H.nRecs = get(hObject,'value')+1; %number of rectangles to draw
> H.xPos=15-12*cos(alpha); H.yPos=15+12*sin(alpha); %Position of Rectangles
> for i=1:H.nRecs
> H.hRec(i)=rectangle(...);
> end
> guidata(H.hFig, H) %save app data

> okay, now H.hRec is stored in H, but now H.hnRecsPop is not stored. how can i get this fixed? how can i store both, H.hRec and H.hnRecsPop?

simply by adding this statment IN your callback function

function nRecsPop_Callback(hObject,varargin)
     H=guidata(gcbf); %load app data
     H.hnRecsPop=hObject;
% ...
     guidata(H.hFig, H) %save app data
end

us

Subject: saving handles

From: Roland

Date: 22 Jul, 2009 18:50:18

Message: 7 of 9

> simply by adding this statment IN your callback function
>
> function nRecsPop_Callback(hObject,varargin)
> H=guidata(gcbf); %load app data
> H.hnRecsPop=hObject;
> % ...
> guidata(H.hFig, H) %save app data
> end
>
> us

YES, thats it!
now i got this fixed, thank?s a lot for your help!

just one more question, do you think it is clever to save a lot of handles and data with the guidata command, or is there a better, dedicated function for storing data, that is passed to other uicontrols?

Subject: saving handles

From: us

Date: 22 Jul, 2009 19:01:19

Message: 8 of 9

"Roland "
> just one more question, do you think it is clever to save a lot of handles and data with the guidata command, or is there a better, dedicated function for storing data, that is passed to other uicontrols?...

it's well behaved and - from a syntactic point of view - easy to handle...

other (often more cumbersome) containers are

     help setappdata; % <- and siblings
% and the USERDATA prop of most graphics handles including the root (= 0)

us

Subject: saving handles

From: Roland

Date: 22 Jul, 2009 21:41:01

Message: 9 of 9

"us " <us@neurol.unizh.ch> wrote in message <h47nlv$aao$1@fred.mathworks.com>...
> "Roland "
> > just one more question, do you think it is clever to save a lot of handles and data with the guidata command, or is there a better, dedicated function for storing data, that is passed to other uicontrols?...
>
> it's well behaved and - from a syntactic point of view - easy to handle...
>
> other (often more cumbersome) containers are
>
> help setappdata; % <- and siblings
> % and the USERDATA prop of most graphics handles including the root (= 0)
>
> us

okay, thats everything i needed, again, thanks a lot!

\Roland

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us