String in an axis label that varies depending on code

I am trying to include a ylabel on a plot that has three possibilities: if x=1, that axis title should be "Daily mean temperature", if x=2, it should be "Daily minimum temperature, if x=3, it should be "Daily maximum temperature". That should be an easy one but I can't seem to find the right answer. Thanks.

 Respuesta aceptada

the cyclist
the cyclist el 21 de Oct. de 2023
Editada: the cyclist el 21 de Oct. de 2023
There are many possible ways to achieve this. One way is to store the strings in an array, and then index into that array using x.
x = 1; % Will also work for x==2 or x==3, giving the different strings
temperatureStr = ["mean", "minimum", "maximum"];
figure
scatter(rand(5,1),rand(5,1))
title(sprintf("Daily %s temperature",temperatureStr(x)))
If you share some broader context of your code, I expect there is a better way to track this info, because there is likely some redundancy in how you are using this indexing..

4 comentarios

That works (more subtle than I expected). Thanks!
Hello @Paul Barrette, if this answer solved your problem, please consider accepting the answer.
Accepting the answer indicates that your problem has been solved (which can be helpful to other people in future) and it awards the volunteer with reputation points for helping you.
You can accept only 1 answer for a question, but you can vote for as many answers as you want. Voting an answer also provides reputation points.
Paul Barrette
Paul Barrette el 21 de Oct. de 2023
Editada: Paul Barrette el 21 de Oct. de 2023
I thought I had accepted (by pressing 'Accept' in the email), but didn't pay enough attention to the post. Thanks for this, @Dyuman Joshi. So volunteers get 'reputation points' - I didn't know.
I believe clicking on the "Accept" in the email redirects you to the post, where you can accept the answer formally.
Yes, earning reputation points grants Privileges to the volunteers. Also, as one gains reputation points, their 'User level' goes up. Higher level volunteers (are likely to) have vast experience with MATLAB and possess intricate knowledge and understanding of the language.
Thus, higher level volunteers are considered trusted and reliable contributors to the MATLAB Answers forum.
@the cyclist, who has answered your question, has been helping others (including you and me) since 2011! (This is not supposed to be read as a factorial sign :P)
And the contributions he has made, have earned him the MVP status.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with MATLAB en Centro de ayuda y File Exchange.

Productos

Versión

R2023b

Etiquetas

Preguntada:

el 21 de Oct. de 2023

Comentada:

el 21 de Oct. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by