In a problem I had to create a animation of the function

sin(x-a)
were a ranged from 0 to 8pi
Code:
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

pause
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.

Here's my attempt at what I thought would produce what I was looking for.
Code:
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)));
pause
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.

also note that i don't have pause there at the end of my loop i just added that in there when i was trying to slow down the animation to see if something was wrong but nothing was found

I have not checked in great detail your code, but something does not look right to me, and it could be the culprit.

The third line from the end reads:

title(fprintf('sin(x-%s)\n',num2str(a)));
Since fprintf() takes at least 3 arguments:
1. file handle
2. formatting string
3. list of arguments

This is not the one to use.
You may have meant to use sprintf which formats an argument through the format string and returns a string.

If I were you, I would replace the original statement to:
title(sprintf('sin(x-%.2f)\n',a))
since a is numeric, a float conversion should do the job.

I would also replace your first "title" statement with the same line, eliminating the second one, since a=0 at the beginning.

Check if it works out, and let us know what you got.

In your code, the issue lies in the line where you set the title using `fprintf`.

Here's the problematic line from your code:

```matlab
title(fprintf('sin(x-%s)\n',num2str(a)));
```

The issue is that `fprintf` returns the number of characters printed, not the formatted string itself. Therefore, when you pass the result of `fprintf` to `title`, it interprets the number (e.g., 14, 13, 15) as the title instead of the expected string.

To fix it, you can use `sprintf` instead of `fprintf` to create the formatted string and then pass it to `title` directly. Here's the corrected line:

```matlab
title(sprintf('sin(x-%s)\n',num2str(a)));
```

Now, `sprintf` will create the formatted string using the value of `a` converted to a string by `num2str`. The resulting string will be correctly passed to `title`, and you should see the expected title on your graph.

Here's the updated code snippet:

```matlab
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 + 0.1*pi;
y = sin(x-a);
set(g,'ydata',y)
title(sprintf('sin(x-%s)\n',num2str(a))); % Corrected line
drawnow
pause
end
```

With this change, the title of your graph will update in each iteration of the loop based on the value of `a`.