MATLAB Answers

How do I use fill to create continuous error bars?

11 views (last 30 days)
Justin Osborn
Justin Osborn on 13 Oct 2020
Commented: Adam Danz on 13 Oct 2020
I was using the code to create continous error bars as referenced here, but could not get it to work for my dataset. Whenever I try and graph the data as a shaded area, I instead only get lines at each data point. I was able to get it to work for the values referenced in the example code, but cannot get it to work for my data set. How do I fix this?
n = 0;
for f = 500:25:1000
n = n+1;
freq(n) = f;
[Z_e_tot(n), Z_e_tot_err(n), R_e_tot(n), R_e_tot_err(n), X_e_tot(n), X_e_tot_err(n)] = imped_calc(f);
disp(f);
end;
figure()
hold on;
new_err = 1.*Z_e_tot_err;
fill([freq; flipud(freq)],[Z_e_tot-new_err;flipud(Z_e_tot+new_err)],[.9 .9 .9], 'linestyle', 'none');
line(freq, Z_e_tot);

  2 Comments

Cris LaPierre
Cris LaPierre on 13 Oct 2020
It looks like we need your imped_calc function to be able to see what is happening. Can you attach it? Use the paperclip icon.
Justin Osborn
Justin Osborn on 13 Oct 2020
Hey Cris. I just attached it. I don't think it really matters, but it will produce a vector of doubles that is the same length of the freq vector.

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 13 Oct 2020
Edited: Adam Danz on 13 Oct 2020
Having the imped_calc function matters because now we can see the size and shape of the outputs.
  • freq is a 1x21 vector.
  • Z_e_tot and new_err are a 1x21 vectors
so the first input to fill() is [freq, flipud(freq)] which results in a 1x42 vector.
The second input is [Z_e_tot-new_err; flipud(Z_e_tot+new_err)] which results in a 2x21 vector.
This causes an unreported error,
Error using fill
Vectors must be the same length.
What you probably wanted to do is,
fill([freq, fliplr(freq)],[Z_e_tot-new_err,fliplr(Z_e_tot+new_err)],[.9 .9 .9], 'linestyle', 'none');
  • The first two inputs are row vectors so you should horizontally concatenat them with ",". The 2nd input in your attempt used vertical concatenation with ";".
  • Because they are row vectors you want to use fliplr, not flipud.

  4 Comments

Show 1 older comment
Adam Danz
Adam Danz on 13 Oct 2020
That's not what I suggested. Look more closesly at the line of code in my answer and see how it differs from what you're trying.
If you're not getting an error when running the exact version of the code provided in your question, please let us know what release of Matlab you're using.
Here's the result when you implement the suggestion correctly,
Justin Osborn
Justin Osborn on 13 Oct 2020
Oh yes, I see what you mean. It is working now. Thank you for all of the help!
Adam Danz
Adam Danz on 13 Oct 2020
Glad I could help. As Cris LaPierre has shown, the inputs can also be column vectors of the same length. What matters is that the sizes of the first two inputs agree in such a way that Matlab can interpret the vertices of the patch object.

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 13 Oct 2020
Having your functino may not technically be necessary, but it sure makes it easier to ensure we are seeing what you see. In this case, your code is creating row vectors, but the solution you are using expects column vectors. Having your code create column vectors is all that is necessary to get your code to run. Instead of (n), use (n,1).
n = 0;
for f = 500:25:1000
n = n+1;
freq(n,1) = f;
[Z_e_tot(n,1), Z_e_tot_err(n,1), R_e_tot(n,1), R_e_tot_err(n,1), X_e_tot(n,1), X_e_tot_err(n,1)] = imped_calc(f);
end
new_err = 1.*Z_e_tot_err;
fill([freq; flipud(freq)],[Z_e_tot-new_err;flipud(Z_e_tot+new_err)],[.9 .9 .9], 'linestyle', 'none');
line(freq, Z_e_tot);

  0 Comments

Sign in to comment.

Tags

Products

Community Treasure Hunt

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

Start Hunting!