How to add: min, max, mean, median, st dev values to histogram legend

I would like to add information about min, max, mean, median, and st dev to the histogram. I tried to do this using the following script:
% Import data
A = importdata('C:\my_data.tif');
% Generate histogram
histogram(A,'BinLimits',[0,1000]);
% Calculate the min, max, mean, median, and standard deviation
min=min(A);
max=max(A);
mn=mean(A);
md=median(A);
stdv=std(A);
% Create the labels
minlabel=sprintf('Min -- %3.2d', min);
maxlabel=sprintf('Max -- %3.2d', max);
mnlabel=sprintf('Mean -- %3.2d', mn);
mdlabel=sprintf('Median -- %3.2d', md);
stdlabel=sprintf('Std Deviation -- %3.2d', stdv);
% Create the textbox
h=annotation('textbox',[0.58 0.75 0.1 0.1]);
set(h,'String',{minlabel, maxlabel,mnlabel, mdlabel, stdlabel});
However, every time when I run the script the last line of the script crushed in Matlab (see attached message). Does anyone know how to solve the error? Or is there another way to add min, max info to the histogram? Thanks in advance for the answer.

답변 (2개)

I'd contact tech support if the crash is reproducible.
A minimal working example works for me:
histogram(rand(100,1))
annotation('textbox','String',{'hello', 'world'})
Karolina
Karolina 2015년 7월 6일
편집: Karolina 2015년 7월 6일

0 개 추천

Thank you for the answer. Yes, the error, unfortunately, occurs every time.
Your script works well, but I have to generate a lot of histograms, so I would like to have the possibility to add the min, max, mean, median, stdv values automatically instead of typing them every time manually. Is there a way to do this?

댓글 수: 4

What you have above should work. In fact, if I copy it and run it with a synthetic A matrix, it does. So it probably has something to do with your A matrix. What does your A matrix look like (size, are there nan/inf values, class etc.)?
Karolina
Karolina 2015년 7월 7일
편집: Karolina 2015년 7월 7일
Thank you for your answer and for verification of the code. So the problem is probably with: matlab or my data.
Below you can find the properties od my data.
I tried also to use the code dor different data but the Matlab also crushed
Contact tech support! It could quite possible by a simple graphics driver update or something specific to your machine that they can help with!
I already did it. I hope they will answer soon.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

질문:

2015년 7월 6일

댓글:

2015년 7월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by