How do I put the errorbar in the plot?

3 views (last 30 days)
Agnese Chini
Agnese Chini on 6 Oct 2021
Answered: yanqi liu on 6 Oct 2021
Hi, I have this program:
My program analyzes 21 different images and crops (for each image) 6 ROIs. The ROIs are cropped from a reference image and the coordinates are used to crop the ROIs to the other images. Then for each ROI the mean (saved in a 6x21 matrix named as meanbasket) and the standard deviation (saved in another 6x21 matrix named as devstdbasket) are made. The matrices are 6x21 because 6 is the number of ROIs and 21 the number of images. After that the values ​​of the means are compared with the real values ​​and the results are saved in another 6x21 matrix (errbasket).
My problem are:
  • Preallocating devstdbasket before entering the loop by using zeros, ones, cell, or a similar function. Because MatLab suggest me that because of the different size of the matrix
  • Plot in one graph the matrix errbasket (The matrix must be plotted transposed, as I need the curves of the graph to be the rows of the matrix and not the columns) with the error bars (as in the figure below).
The values in devstdbasket determine the lengths of each error bar above and below the data points, so the total error bar lengths are double the devstdbasket values.
srcFile = dir('C:\Users\agnes\Pictures\PP4 TGC-MED RD\PP4 TGC-MED RD 100\*.dcm');
pathname = ('C:\Users\agnes\Pictures\PP4 TGC-MED RD\PP4 TGC-MED RD 100\');
% define the images
numberofimages = 21;
numberofroi = 6; %define the cist or the rows of the future matrix
%I work on the reference image
%In a void cell I will store the coordinates of the ROIs
R = nan(numberofroi,4);
for nr = 1:numberofroi
h = drawrectangle(gca); wait(h); %I crop the ROIs freehand on the reference Image
roibasket = cell(numberofroi,numberofimages);
meanbasket = nan(size(roibasket));
for ni = 1:numberofimages
for nr = 1:numberofroi
roibasket{nr,ni} = imcrop(pileofimages,R(nr,:));
meanbasket(nr,ni) = mean( pileofimages(masks{nr}) ); %I do the mean of the ROIs
devstdbasket(nr,ni) = std2 ( pileofimages(masks{nr}) ); %I do the standard Deviation and my problem is That the size of the indicated variable appears to be changing with each loop iteration.
rvalue = [-9; -6; -3; 3; 6; 9]; %This is the array of the true values
errbasket = (meanbasket - rvalue); %I confront the result save in the matrix of the mean with the true values
hold on
errbasketT = transpose(errbasket); %I need to plot one curves for each rows (instead of columns, so I transpose)
% figure; plot(errbasketT);hold on %Till here I have the plot of the matrix
% named as errbasket (transpose)
%But I have to put in the same graph the Line plot with error bars, the
%value of the error bars is in the devstdbasket (standard deviation)
x = 1:1:21;
figure; errorbar(x,errbasketT,devstdbasket); hold on
In this graph you can see the error bars that I was talking about
And it would be useful if you added a legend to the graph to be able to distinguish the curves.
Thank you

Accepted Answer

yanqi liu
yanqi liu on 6 Oct 2021
sir,i can not get your dcm files, so use an example, please check the follow code, thanks.
clc; clear all; close all;
x = [1:10:100; 1:10:100];
y = [20 30 45 40 60 65 80 75 95 90; 20 30 45 40 60 65 80 75 95 90];
err = [8*ones(1,size(y,2)); 2*rand(1,size(y,2))];
he = errorbar(x',y',err');
legend(he, {'no 1', 'no 2'})

More Answers (0)

Community Treasure Hunt

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

Start Hunting!