Clear Filters
Clear Filters

How to specify equal distance interval in matlab plots

8 views (last 30 days)
I have a set of data. I want to plot this data at equal intervals but this data is clustered at some location (top and bottom axis). I am using this command (1:3:length(vel)) but this is reading every 3rd data, but at a specific location I want more data and on the other side I want less data. How can I do this?
I have attached a code below mentioning the data and the figure also. In this figure I want less data on both top and bottom (Important to show this also on graph) x-axis while in between the axises I want more data point. How Can I do this.
data = xlsread("hello.xlsx");
vel = data(:,1);
Min = data(:,3);
Mod = data(:,5);
Maj = data(:,7);
Col = data(:,9);
plot(vel,Min,'--k+', 'MarkerSize',7,'linewidth', 1.5,'MarkerIndices',1:3:length(vel))
hold on
plot(vel,Mod,'--bo', 'MarkerSize',7,'linewidth', 1.5,'MarkerIndices',1:4:length(vel))
plot(vel,Maj,'--gs', 'MarkerSize',7,'linewidth', 1.5,'MarkerIndices',1:5:length(vel))
plot(vel,Col,'--r*', 'MarkerSize',7,'linewidth', 1.5,'MarkerIndices',1:6:length(vel))
hold off
%grid on
Thanks in advance...

Accepted Answer

Antoni Garcia-Herreros
Antoni Garcia-Herreros on 29 Mar 2023
Hello,
You could try something like this:
data = xlsread("hello.xlsx");
vel = data(:,1);
Min = data(:,3);
Mod = data(:,5);
Maj = data(:,7);
Col = data(:,9);
step=20; %Sets the space between the non-important data
th=0.001; % Threshold to accept the minimum distance between consecutive points
Diff_Min=Min(1:numel(Min)-1,1)-Min(2:numel(Min),1); %Array storing the difference between consecutive points
i_Min_first=find(abs(Diff_Min)>th,1); % First index with a difference larger than the threshold
i_Min_last=find(abs(Diff_Min)>th,1,'last'); % Last index with a difference larger than the threshold
V_Min=[vel(1:step:i_Min_first);vel(i_Min_first+1:i_Min_last);vel(i_Min_last+1:step:end)]; %vel array with the values of the 3 regions
Y_Min=[Min(1:step:i_Min_first);Min(i_Min_first+1:i_Min_last);Min(i_Min_last+1:step:end)]; %Min array with the values of the 3 regions
Diff_Mod=Mod(1:numel(Mod)-1,1)-Mod(2:numel(Mod),1);
i_Mod_first=find(abs(Diff_Mod)>th,1);
i_Mod_last=find(abs(Diff_Mod)>th,1,'last');
V_Mod=[vel(1:step:i_Mod_first);vel(i_Mod_first+1:i_Mod_last);vel(i_Mod_last+1:step:end)];
Y_Mod=[Mod(1:step:i_Mod_first);Mod(i_Mod_first+1:i_Mod_last);Mod(i_Mod_last+1:step:end)];
Diff_Maj=Maj(1:numel(Maj)-1,1)-Maj(2:numel(Maj),1);
i_Maj_first=find(abs(Diff_Maj)>th,1);
i_Maj_last=find(abs(Diff_Maj)>th,1,'last');
V_Maj=[vel(1:step:i_Maj_first);vel(i_Maj_first+1:i_Maj_last);vel(i_Maj_last+1:step:end)];
Y_Maj=[Maj(1:step:i_Maj_first);Maj(i_Maj_first+1:i_Maj_last);Maj(i_Maj_last+1:step:end)];
Diff_Col=Col(1:numel(Col)-1,1)-Col(2:numel(Col),1);
i_Col_first=find(abs(Diff_Col)>th,1);
i_Col_last=find(abs(Diff_Col)>th,1,'last');
V_Col=[vel(1:step:i_Col_first);vel(i_Col_first+1:i_Col_last);vel(i_Col_last+1:step:end)];
Y_Col=[Col(1:step:i_Col_first);Col(i_Col_first+1:i_Col_last);Col(i_Col_last+1:step:end)];
plot(V_Min,Y_Min,'--k+', 'MarkerSize',7,'linewidth', 1.5)
hold on
plot(V_Mod,Y_Mod,'--bo', 'MarkerSize',7,'linewidth', 1.5)
plot(V_Maj,Y_Maj,'--gs', 'MarkerSize',7,'linewidth', 1.5)
plot(V_Col,Y_Col,'--r*', 'MarkerSize',7,'linewidth', 1.5)
hold off
  1 Comment
Raj Arora
Raj Arora on 29 Mar 2023
Thanks antoni, I understood what you are doing, basically dividing each plot into 3 different sections and then plotting those. Initially I also thought in the same way but not able to implement it. Thanks once again.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots 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!