I have a basic plot as follows. How can I display mean and std of X, Y, and Z in the legend?
Plot1=plot(T,X)
hold on
Plot2=plot(T,Y)
hold on
Plot3=plot(T,Z)
legend([Plot1, Plot2, Plot3], 'X','Y','Z')

 Respuesta aceptada

Rik
Rik el 12 de Jun. de 2018
Editada: Rik el 12 de Jun. de 2018
Just use sprintf and the mean and std functions:
Plot1=plot(T,X);
hold on
Plot2=plot(T,Y);
Plot3=plot(T,Z);
legend([Plot1, Plot2, Plot3],...
sprintf('X (mean=%.1f,std=%.1f)',mean(X,'omitnan'),std(X,'omitnan')),...
sprintf('Y (mean=%.1f,std=%.1f)',mean(Y,'omitnan'),std(Y,'omitnan')),...
sprintf('Z (mean=%.1f,std=%.1f)',mean(Z,'omitnan'),std(Z,'omitnan')))

3 comentarios

alpedhuez
alpedhuez el 12 de Jun. de 2018
Editada: alpedhuez el 12 de Jun. de 2018
What to do if Y contains NaN?
Adam
Adam el 12 de Jun. de 2018
Editada: Adam el 12 de Jun. de 2018
That's a problem of your data, not the code to put the results in the legend. Try using nanmean and nanstd or use the 'omitnan' flag.
alpedhuez
alpedhuez el 12 de Jun. de 2018
Editada: alpedhuez el 12 de Jun. de 2018
Thank you.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Productos

Etiquetas

Preguntada:

el 12 de Jun. de 2018

Editada:

Rik
el 12 de Jun. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by