Adding Horizontal (error) lines to a plot

I have a plot (plot(e)) which shows displacement of 3 different positions (this plots well). I am trying to add horizontal error lines when the max value for each position is => 0.05%. However, I am unable to figure out how to have the error lines appear within my plot.
allData = xlsread('FILE NAME'); % File Directory (.xls)
ay = allData(:,3); % y array of hip
a1 = allData(1,3); % first cell of y array of hip
a2 = a1+(a1*0.05); % 0.05% error line
by = allData(:,4); % y array of knee
b1 = allData(1,4); % first cell of y array of knee
b2 = b1+(b1*0.05); % 0.05% error line
cy = allData(:,5); % y array of foot
c1 = allData(1,5); % first cell of y array of foot
c2 = c1+(c1*0.05); % 0.05% error line
e = [ay,by,cy];
if max(ay) >= a2 % Position greater than 0.05% error (a2)
plot( [a2(1) a2(end)], 'r'); % error reference line
elseif max(by) >= b2 % Position greater than 0.05% error (a2)
plot( [b2(1) b2(end)], 'r'); % error reference line
elseif max(cy) >= c2 % Position greater than 0.05% error (a2)
plot( [c2(1) c2(end)], 'r'); % error reference line
end
plot(e)
title('Sway (Backswing)');
ylabel('Position (mm)');
xlabel('Time (ms)');
legend('Hip', 'Knee', 'Foot');

1 个评论

Please introduce yourself to the
Code{}
radio button to format your code.

请先登录,再进行评论。

回答(1 个)

I don’t have your data file, so I can’t run your code.
I’m not certain what you intend with your plot statements. See if something like this works:
plot(xlim, [a2(1) a2(end)], 'r') % error reference line
Note that xlim is a (1 x 2) vector of the x-axis limits, so although I cannot test it with your data, this should work. It should also work if you re-define the x-axis limits.

4 个评论

I am attaching the data file for this code. My issue is that the code produces a nice figure plot with three distinct lines, all representing one of the variables (hip, knee, foot). I am wanting to include the horizontal (error) lines where a variable goes beyond a specific position, in this case it would be 0.05% error limit. Even including xlim still doesn't seem to display an error bar when a variable is out of limits.
I don’t see the need for the if block, especially since it is preventing the reference lines from plotting (the conditions are never met). If you want to plot the reference lines, this works:
figure(1)
plot(e)
hold on
plot(xlim, [a2(1) a2(end)], 'r'); % error reference line
plot(xlim, [b2(1) b2(end)], 'r'); % error reference line
plot(xlim, [c2(1) c2(end)], 'r'); % error reference line
hold off
title('Sway (Backswing)');
ylabel('Position (mm)');
xlabel('Time (ms)');
legend('Hip', 'Knee', 'Foot');
Also, ‘a2’ and the others are scalars, so there is no need to subscript them. I left the subscripts in, since I don’t know what you want to do, however they are not necessary.
Amazing! Thanks for the assistance.
My pleasure!
If my Answer helped solve your problem, please Accept it!

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Annotations 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by