MATLAB Answers

Shade area between two curves and maintain central solid line in shaded area

4 views (last 30 days)
Matt King
Matt King on 7 May 2017
Answered: Chad Greene on 6 Sep 2017
Hi MATLAB community
I am knew to programing in MATLAB and using it to assist in streamlining some graphing process's with data (I am a biomechanics)
I have written a script to plot outputs from the program I use (which initially exports to excel) and have placed it below. I am wondering how to go about writing script to shade the area between y1sdupper and y1sdlower in a transparent red, as well as shade the area between y2sdupper and y2sdlower with a transparent blue (these are the +/- 1sd of the mean) while still keeping the solid y1 and y2 line visible. Any assistance will be greatly appreciated.
fprintf('Please note: Excel file setup:\n')
fprintf('Column A = x1\n')
fprintf('Column B = y1\n')
fprintf('Column C = y1SD\n')
fprintf('Column D = y2\n')
fprintf('Column e = y2SD\n')
fprintf('please ensure the excel file is saved in your "Current Folder"\n\n')
fprintf('If you have already completed this, please wait the program will sart in 5 seconds:\n\n')
pause (1)
prompt = {'Enter file name:'};
dlg_title = 'Input';
num_lines = 1;
defaultans = {'Book1'};
a = inputdlg(prompt,dlg_title,num_lines,defaultans);
file = strjoin(a);
data = xlsread(file);
x1 = data(:,1);
y1 = data(:,2);
y1sdupper = data(:,2) + data (:,3);
y1sdlower = data(:,2) - data (:,3);
y2 = data(:,4);
y2sdupper = data(:,4) + data (:,5);
y2sdlower = data(:,4) - data (:,5);
plot(x1, y1, 'r', x1, y2, 'b', x1, y1sdupper, 'r--', x1, y1sdlower, 'r--', x1, y2sdupper, 'b--', x1, y2sdlower, 'b--')
Cheers Matt
  1 Comment
Star Strider
Star Strider on 7 May 2017
The patch function will shade the area you want.
I cannot run your code, and I have no idea what you actually want to do.

Sign in to comment.

Accepted Answer

Chad Greene
Chad Greene on 6 Sep 2017
Hi Matt,
I recommend Kelly Kearney's boundedline function. Just give it the central line and error values, and it figures out the rest. Like everything I've seen by Kelly, it's well designed and well documented.

More Answers (1)

Maitreyee Mordekar
Maitreyee Mordekar on 16 May 2017
Maybe the following links will help you implement your requirement:
Hope that helps!

Community Treasure Hunt

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

Start Hunting!