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:
Automatically positioning text in plots

Subject: Automatically positioning text in plots

From: Jeff

Date: 19 Aug, 2011 19:08:09

Message: 1 of 4

Hello. Is there a good way to determine the height of text in a plot and position it outside of the axes?

animate.m
---------
close all; figure;
[m,N]=size(s1po);
mx=max(max(s1po)); mn=min(min(s1po));
xs=[0 N+1 mn mx];
stepsize=ceil(m/1024);

for ii=1:stepsize:m
    plot(s1po(ii,:),'o'); axis(xs);
    txt=sprintf('t=%d/%d',ii,m);
    text(1,xs(4),txt,'BackgroundColor','white', 'EdgeColor','red','VerticalAlignment','cap');
    xlabel('Mass No.')
    ylabel('Displacement')
    drawnow;
    if ii==1
        pause;
        shg;
    end
end
---------

Variable s1po contains position data in time (each row is a timestep, and each column represents the position of a mass). I want to animate it by plotting the masses' positions at each timestep.

I also want to display some text on the plot which reads something like "timestep=currentStep/totalSteps"; I create that string using "txt=sprintf('t=%d/%d',ii,m);" (the second line in the loop below).

I position the text using the plot's "text" command (the third line in the loop below). I position it at location (1,data's_maximum_vertical_value) and I want it to display outside of the plot. However, it is overlaying part of the plot area.

I can move it out of the plot by saying something like "text(1,xs(4)+10,...)", but that does not work consistently for different maximum values and different sized windows.

Is there a more automated way to determine the height of the text and position it outside of the axes?

Thanks very much.
Jeff

Subject: Automatically positioning text in plots

From: Kirill

Date: 23 Aug, 2011 21:50:11

Message: 2 of 4

On Aug 19, 3:08 pm, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
wrote:
> Hello. Is there a good way to determine the height of text in a plot and position it outside of the axes?
>
> animate.m
> ---------
> close all; figure;
> [m,N]=size(s1po);
> mx=max(max(s1po)); mn=min(min(s1po));
> xs=[0 N+1 mn mx];
> stepsize=ceil(m/1024);
>
> for ii=1:stepsize:m
>     plot(s1po(ii,:),'o'); axis(xs);
>     txt=sprintf('t=%d/%d',ii,m);
>     text(1,xs(4),txt,'BackgroundColor','white', 'EdgeColor','red','VerticalAlignment','cap');
>     xlabel('Mass No.')
>     ylabel('Displacement')
>     drawnow;
>     if ii==1
>         pause;
>         shg;
>     end
> end
> ---------
>
> Variable s1po contains position data in time (each row is a timestep, and each column represents the position of a mass). I want to animate it by plotting the masses' positions at each timestep.
>
> I also want to display some text on the plot which reads something like "timestep=currentStep/totalSteps"; I create that string using "txt=sprintf('t=%d/%d',ii,m);" (the second line in the loop below).
>
> I position the text using the plot's "text" command (the third line in the loop below). I position it at location (1,data's_maximum_vertical_value) and I want it to display outside of the plot. However, it is overlaying part of the plot area.
>
> I can move it out of the plot by saying something like "text(1,xs(4)+10,...)", but that does not work consistently for different maximum values and different sized windows.
>
> Is there a more automated way to determine the height of the text and position it outside of the axes?
>
> Thanks very much.
> Jeff

I cannot run it:

??? Undefined function or variable 's1po'.

Error in ==> test_mathgroup1 at 49
[m,N]=size(s1po);

Subject: Automatically positioning text in plots

From: Jeff

Date: 23 Aug, 2011 22:29:29

Message: 3 of 4

Kirill <kirillandreev@gmail.com> wrote in message <66092304-5acf-46b0-9f6b-4a41968fb7d9@h9g2000vbr.googlegroups.com>...
> On Aug 19, 3:08 pm, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
> wrote:
>
> I cannot run it:
>
> ??? Undefined function or variable 's1po'.
>
> Error in ==> test_mathgroup1 at 49
> [m,N]=size(s1po);

I uploaded a s1po.mat to http://www.mediafire.com/file/669o7jm5blz8xua/s1po.mat. If you can wade through the popups, you can use that. I think you can load that into Matlab using "load s1po.mat"

If not, post back, I can extract a code snip that will create an s1po for you.

Thanks for looking at it.
Jeff

Subject: Automatically positioning text in plots

From: Kirill

Date: 25 Aug, 2011 20:55:27

Message: 4 of 4

On Aug 23, 6:29 pm, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
wrote:
> Kirill <kirillandr...@gmail.com> wrote in message <66092304-5acf-46b0-9f6b-4a41968fb...@h9g2000vbr.googlegroups.com>...
> > On Aug 19, 3:08 pm, "Jeff " <spREMOVEHITSjef...@SIGNoptonline.net>
> > wrote:
>
> > I cannot run it:
>
> > ??? Undefined function or variable 's1po'.
>
> > Error in ==> test_mathgroup1 at 49
> > [m,N]=size(s1po);
>
> I uploaded a s1po.mat tohttp://www.mediafire.com/file/669o7jm5blz8xua/s1po.mat. If you can wade through the popups, you can use that. I think you can load that into Matlab using "load s1po.mat"
>
> If not, post back, I can extract a code snip that will create an s1po for you.
>
> Thanks for looking at it.
> Jeff

The easiest way, perhaps, is to use normalized units as in the example
below. There is also Extent property that could be exploited.

Kirill

clearall

load e:\~\s1po


close all; figure;
 [m,N]=size(s1po);
 mx=max(max(s1po)); mn=min(min(s1po));
 xs=[0 N+1 mn mx];
 stepsize=ceil(m/1024);


 for ii=1:stepsize:m
     plot(s1po(ii,:),'o'); axis(xs);

     % randomize y-limits
     yl = ylim;
     yl(1) = randn + yl(1);
     yl(2) = randn + yl(2);

     if diff(yl) > 0
         ylim(yl);
     end

     txt=sprintf('t=%d/%d',ii,m);
     text(0.02, 1.02,txt,'BackgroundColor','white',
'EdgeColor','red','VerticalAlignment','cap', 'Units', 'normalized');

     %text(1,xs(4),txt,'BackgroundColor','white',
'EdgeColor','red','VerticalAlignment','cap');


     xlabel('Mass No.')
     ylabel('Displacement')
     drawnow;
     if ii==1
         pause;
         shg;
     end
 end

Tags for this Thread

No tags are associated with 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