# Doug's MATLAB Video Tutorials

## Basics: Making a title based on a variable value

Internet explorer users: Please follow the permalink at the bottom of this post to see the correct video… We are working on this! Sorry! This short video shows how to convert variable values to strings. This is needed for making titles, xlabels, ylabels and other functions that expect a string for input.

### 11 Responses to “Basics: Making a title based on a variable value”

1. matt fig replied on :

For any who might be interested in why you got an asterisk.

char(42)
double(‘*’)

Matlab did a little num2str on it’s own, though not in the way which was desired!

2. Petr Pošík replied on :

I do not know why (maybe because I have background in those old fashioned languages like C), for a long time I prefered to write

 title( sprintf( '%s: %d', str, num ) ); 

I have learned about this ['a' 'b'] way of doing it rather recently, but I have not get used to that yet. Although a bit more cryptic, sprintf gives me more freedom in formating the result.

3. dhull replied on :

Petr,

It is funny, I give a “Doug’s impossible MATLAB quiz” to many of the new hires in technical support. Someone started doing the sprintf thing, and I guessed that she was a C programmer. She was all like “How did you know that?”

Funny how that works. I have never internalized the use of sprintf, even after 15 years of programming MATLAB!

-Doug

4. dhull replied on :

@matt,

Yes, I knew MATLAB was doing something like that. It was not the point of the video, so I just ignored it. Glad to see someone noticed that in there!

Doug

5. Joel Quintana replied on :

Your video was a lot of help.
You explained it awesome.
Than you.

6. Umberto replied on :

Awesome explanation! Sometimes these little things are forgotten even by beginner text books. Thanks for the great work, and for helping me :)

7. Simeon replied on :

Thanks, Doug. Saved me a lot of time.

8. Swaprava replied on :

Wonderful tutorial, was looking for this for quite sometime!

9. Savka replied on :

Thank you very much! I have spent a few hours trying to figure out what was the problem with the title. There is no book that I could find with explanation of this ‘simple’ problem.

10. Nafus replied on :

Wow!this really works.Thanks a lot. I have been strugling to combine two strings and two variables in a title using sprintf

11. DeltaFoxtrot replied on :

Thank you so much for this video, I am making four graphs with subplot in a for loop with a changing variable that I needed in the title. I was trying title(‘Thickness: ‘, var) to no avail! You solved my problem.

 Name (required) E-mail (required, will not be published) Website (optional) Spam protection (required): What is 4 + 7 ?

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>


If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).

Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

These postings are the author's and don't necessarily represent the opinions of MathWorks.