Clear Filters
Clear Filters

Trimming a curve - how to find nearest number and replace

2 views (last 30 days)
I have the x and y coordinates for a bathymetric curve eg...
x y
0 1.36
0.03 -0.92
0.13 -1.68
1.69 -3.72
6.63 -3.35
9.18 -3.36
20.03 -3.26
31.75 -2.93
40.15 -1.70
46.21 0.10
69.97 2.26
I would like to trim this curve to a certain level line with at y=-0.15 and x from 0 to 45 I need to both replace the first coordinate with (0,-0.15) and make the final coordinate (45,-0.15) removing any x,y values greater than this.
I have this for a large number of data sets so of course a loop would be preferential but I cannot work out how to do these operations through code alone. The image below might clarify what I mean a little more, though it was made with different numbers.
Thanks in advance for any help

Accepted Answer

Greg Dionne
Greg Dionne on 30 Mar 2017
Try:
myLimit = -0.15;
iFirst = find(y<myLimit, 1, 'first');
iLast = find(y<myLimit, 1, 'last');
xdesired = x(iFirst:iLast);
ydesired = y(iFirst:iLast);
plot(x, y);
hold on
plot(xdesired, ydesired);
legend('Original','Trimmed')
  3 Comments
Dom Smith
Dom Smith on 31 Mar 2017
With a bit of modification I've made this work thank you
Greg Dionne
Greg Dionne on 31 Mar 2017
OK. good.
For the concatenation I would probably do something like:
% use "," instead of ";" if xdesired and ydesired are row vectors
xdesired = [0; xdesired; 45];
ydesired = [mylimit; xdesired; mylimit];
Glad you got past this.
-G

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 30 Mar 2017
The data you supplied does not look like your plots. But to clamp data to some level, try this:
xy = [...
0 1.36
0.03 -0.92
0.13 -1.68
1.69 -3.72
6.63 -3.35
9.18 -3.36
20.03 -3.26
31.75 -2.93
40.15 -1.70
46.21 0.10
69.97 2.26];
x = xy(:, 1);
y = xy(:, 2);
subplot(2, 1, 1);
plot(x, y, 'b-');
grid on;
yClamped = y; % Initialize
clampLevel = 0.15;
yClamped(y>clampLevel) = clampLevel; % Do the clamping
subplot(2, 1, 2);
plot(x, yClamped, 'b-');
grid on;
  1 Comment
Dom Smith
Dom Smith on 31 Mar 2017
With a bit of modification I made the answer above work for my needs, but thanks so much for your help!

Sign in to comment.

Categories

Find more on Graphics Performance in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!