How do I use fill to create continuous error bars?

55 visualizaciones (últimos 30 días)
Justin Osborn
Justin Osborn el 13 de Oct. de 2020
Comentada: Adam Danz el 13 de Oct. de 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 comentarios
Cris LaPierre
Cris LaPierre el 13 de Oct. de 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 el 13 de Oct. de 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.

Iniciar sesión para comentar.

Respuesta aceptada

Adam Danz
Adam Danz el 13 de Oct. de 2020
Editada: Adam Danz el 13 de Oct. de 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 comentarios
Justin Osborn
Justin Osborn el 13 de Oct. de 2020
Oh yes, I see what you mean. It is working now. Thank you for all of the help!
Adam Danz
Adam Danz el 13 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (1)

Cris LaPierre
Cris LaPierre el 13 de Oct. de 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);

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by