user3509716
user3509716

Reputation: 69

How do I insert values into the title of my graph in MATLAB?

I have a value calculated:

a = 32.123

In the title of my graph I would like to use this number. I have tried this but it is not working:

title('Traffic Flow in A Day: ESTIMATED TOTAL CARS = %d',a);

Upvotes: 0

Views: 952

Answers (3)

The Minion
The Minion

Reputation: 1164

If you want to use a variable you can use num2str() to convert it to str and then you can use strcat() to join it with another string. E.g.:

Title_line= strcat('Traffic Flow in A Day: ESTIMATED TOTAL CARS = ' , num2str(a));
title(Title_line);

or if you don't need the string again you can do it inside the title:

title(strcat('Traffic Flow in A Day: ESTIMATED TOTAL CARS = ',num2str(a)));

The advantage is, that you can use the same string numerous times. If you define it outside of the title.

Upvotes: 0

rayryeng
rayryeng

Reputation: 104565

Another option would be to use num2str, using the output of this and concatenating this together with a character vector. In other words:

title(['Traffic Flow in A Day: ESTIMATED TOTAL CARS = ' num2str(a)]);

This may look more readable rather than using sprintf, but certainly your call!

Upvotes: 2

Marcin
Marcin

Reputation: 239005

This should work:

title(sprintf('Traffic Flow in A Day: ESTIMATED TOTAL CARS = %d',a));

Upvotes: 2

Related Questions