1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

MATLAB - Image Processing - Defining Title of Plot (Pertains to Loops and Animations)

  1. Jul 19, 2011 #1
    THIS WAS RESOLVED

    1. The problem statement, all variables and given/known data
    In a problem I had to create a animation of the function
    sin(x-a)
    were a ranged from 0 to 8pi
    Code (Text):
    x=[-2*pi:.00001*pi:2*pi];
    a=0;
    y=sin(x-a);
    g=plot(x,y);
    grid on;
    title('sin(x-0)')
    set(g,'EraseMode','xor')
    while a<=8*pi
        a=a+.1*pi;
        y=sin(x-a);
        set(g,'ydata',y)
        drawnow
    end
    My code worked fine and did what it was suppose to do. I however was trying to get MATLAB to change the value of a in the title each time it went through the loop and I seem to be having difficulties doing this.

    2. Relevant equations



    3. The attempt at a solution
    Here's my attempt at what I thought would produce what I was looking for.
    Code (Text):
    x=[-2*pi:.00001*pi:2*pi];
    a=0;
    y=sin(x-a);
    g=plot(x,y);
    grid on;
    title('sin(x-a)')
    set(g,'EraseMode','xor')
    while a<=8*pi
        a=a+.1*pi;
        y=sin(x-a);
        set(g,'ydata',y)
        drawnow
        title(fprintf('sin(x-%s)\n',num2str(a)));
    end
    I thought this would work. Before it enters the loop the title would get defined as sin(x-0). I thought that it would then redefine the title in this line
    title(fprintf('sin(x-%f)\n',num2str(a)));
    were num2str(a) would change a, which is defined as a number, into a string of text that could be placed in the title
    sin(x-a)
    That was my logic behind that... I also suppressed the output with the ";" operator

    I thought that this would produce the results I was looking for but the title just gets changed to some kind of number like 14 or 13 or 15 and just stays like that... oddly enough the title I was expecting to get on my graph some how appears in the command window even though I suppressed the output...

    I'm hoping someone can explain what I'm doing wrong.
     
    Last edited: Jul 20, 2011
  2. jcsd
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Can you offer guidance or do you also need help?



Similar Discussions: MATLAB - Image Processing - Defining Title of Plot (Pertains to Loops and Animations)
  1. Mathematica plot (Replies: 0)

Loading...