How to index y-values of a plot for specific x values
10 views (last 30 days)
Show older comments
I have a plot and have specific points of interest. I have idenitified certain breaking points and want to graph these but I don't know how to index the y-values for the specific x-values. 

0 Comments
Answers (1)
BhaTTa
on 29 May 2025
Hey @Luis Martin Romero, Assuming you have your original data (let's call it x_data and y_data) and a list of specific X-values that are your "breaking points," here's how you can find the corresponding Y-values and plot them in MATLAB
% 1. Assume you have your original data:
x_data = 1:0.5:10;
y_data = sin(x_data) + x_data./5 + randn(size(x_data))*0.2; % Some example noisy data
% 2. And you have identified specific "breaking points" (x-values):
breaking_points_x = [2.5, 4.0, 7.5, 9.0];
% --- Get the corresponding Y-values ---
% Method 1: Using 'ismember' (Good for exact matches or when order doesn't matter)
% This is generally the most straightforward for finding Y-values for specific X-values.
[~, idx] = ismember(breaking_points_x, x_data); % Find indices of breaking_points_x in x_data
% ismember returns two outputs:
% 1. A logical array indicating which elements of breaking_points_x are in x_data.
% 2. The index in x_data for each element of breaking_points_x.
% We only need the second output, so we use ~ for the first.
breaking_points_y_ismember = y_data(idx); % Use these indices to get corresponding Y-values
disp('Y-values using ismember:');
disp(breaking_points_y_ismember);
% Method 2: Using 'interp1' (Essential if your breaking_points_x might not be exact matches in x_data)
% If your breaking_points_x values don't perfectly align with x_data, or you want to
% find Y-values for X-values that fall *between* your original data points,
% you'll need interpolation.
breaking_points_y_interp = interp1(x_data, y_data, breaking_points_x);
disp('Y-values using interp1 (interpolation):');
disp(breaking_points_y_interp);
% Note: If a breaking_points_x value is outside the range of x_data, interp1 will return NaN.
% You can specify an 'extrapolation' method for interp1 if you need values outside the range.
% --- Plot these points ---
% Create the initial plot of your original data
figure;
plot(x_data, y_data, 'b-', 'DisplayName', 'Original Data'); % 'b-' for blue line
hold on; % Keep the current plot active to add more elements
% Plot the identified breaking points on top of the original plot
% Using scatter is often good for emphasizing individual points
scatter(breaking_points_x, breaking_points_y_ismember, 100, 'r*', 'filled', 'DisplayName', 'Breaking Points (exact)');
% 'r*' means red asterisks, '100' is marker size, 'filled' fills the marker.
% If you used interpolation and want to plot those:
% scatter(breaking_points_x, breaking_points_y_interp, 100, 'go', 'filled', 'DisplayName', 'Breaking Points (interpolated)');
% Add labels and title for clarity
xlabel('X-axis');
ylabel('Y-axis');
title('Plot with Highlighted Breaking Points');
legend('show'); % Show the legend to identify the different plot elements
grid on;
hold off; % Release the plot to prevent further additions
% --- If you want to plot ONLY the breaking points ---
figure;
plot(breaking_points_x, breaking_points_y_ismember, 'r-o', 'LineWidth', 2, 'MarkerSize', 8, 'DisplayName', 'Breaking Points Only');
xlabel('X-axis');
ylabel('Y-axis');
title('Plot of Breaking Points Only');
legend('show');
grid on;
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!