Plotting graph in a certain range

1 view (last 30 days)
Sohel Rana
Sohel Rana on 21 Sep 2020
Answered: Naga on 26 Feb 2025
Hi,
I have a graph which conatins a lots of peaks within a ceratin range. I would like to plot graphs where each graph will conatin just only one peak.From the follwiing graph, I would like to plot five graphs between 0-50 (graph1), 50-100 (graph2), 100-150(graph3), 150-200(graph4), 200-250(graph5). It should plot five graphs one by one within their corresponding range. I don't want to use axis limit manually as it will take lot of time. Is there any way to use for loop or other loops that will plot five graphs one by one within the above certain range? I would really appreciate your help.
I

Answers (1)

Naga
Naga on 26 Feb 2025
I understand you want to divide the data into five segments, each covering a specific range, and plot each segment in its own graph. You can achieve this by using a 'for' loop to iterate over the specified ranges and plot each segment. Here is a simple example of how to do this in MATLAB:
% Assume `x` is your data's x-axis values and `y` is your data's y-axis values
x = 0:0.1:250;
y = sin(x);
% Define the interval for each plot
interval = 50;
% Calculate the number of plots needed based on the range of x
startValue = min(x);
endValue = max(x);
numPlots = ceil((endValue - startValue) / interval);
% Loop through each range and plot the corresponding segment
for i = 1:numPlots
% Calculate the current range
currentStart = startValue + (i-1) * interval;
currentEnd = min(currentStart + interval, endValue);
% Find indices that fall within the current range
indices = (x >= currentStart) & (x < currentEnd);
% Plot the data within the current range
figure;
plot(x(indices), y(indices));
title(['Plot ', num2str(i), ': Range ', num2str(currentStart), '-', num2str(currentEnd)]);
xlabel('X-axis');
ylabel('Y-axis');
end

Community Treasure Hunt

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

Start Hunting!