How to insert a variable and datetime array into a graph title?

18 vues (au cours des 30 derniers jours)
Brianna Miranda
Brianna Miranda le 2 Mar 2022
Commenté : Star Strider le 2 Mar 2022
I have a graph that I want to include the date&time it was made and the number of data stacks the plot contains on it. The number of stacks is a variable that changes depending on what I input for it. The title for the plot works when I use:
sgtitle([datestr(startTime)]
but I also want to add "Number of stacks: N" to the title.
I have this so far but it isn't working:
sgtitle('Recorded on: ', [datestr(startTime)],'Number of Stacks: ',N)
I want the title to look like this:
Recorded on: dategoeshere, Number of Stacks: N

Réponse acceptée

Star Strider
Star Strider le 2 Mar 2022
It would be easier to use datetime than earlier date versions —
N = 4;
for k = 1:N
subplot(2,2,k)
plot((1:5), randn(1,5))
grid
title(sprintf('Subplot #%d',k))
end
sgtitle(sprintf('Recorded on: %s, Number of Stacks: %d', datetime('now'),N))
See the documentation for sprintf for its details.
.
  2 commentaires
Steven Lord
Steven Lord le 2 Mar 2022
Or perhaps simpler than sprintf:
plot(1:10)
n = 5;
T = datetime('today');
title("This plot with n = " + n + ...
" was generated on " + string(T))
Star Strider
Star Strider le 2 Mar 2022
@Steven Lord — I’d not considered string arrays. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by