How do I use fill to create continuous error bars?
조회 수: 19 (최근 30일)
이전 댓글 표시
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
Cris LaPierre
2020년 10월 13일
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.
채택된 답변
Adam Danz
2020년 10월 13일
편집: Adam Danz
2020년 10월 13일
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
Adam Danz
2020년 10월 13일
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.
추가 답변 (1개)
Cris LaPierre
2020년 10월 13일
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
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

