Here is my code:
for p = 1:length(id_unique)
h=figure;
hold on
j = 1;
for i = 1:1:length(id)
if id_unique(p)==id(i)
h=plot([startdate(i),enddate(i)],[j,j],'ok-');
hold on
j = j + 1;
end
end
grid on
hold off
savefig([plotname,'.fig'])
print(plotname,'-djpeg','-r300')
close
end
% id: integer vector containing 50000 values
% id_unique: sorted unique values from vector 'id'
% startdate: datetime vector containing 50000 dates
% enddate: datetime vector containing 50000 dates
Each elements/values in 'id' vector implies an event where the startdate and the enddate of the event is available in the corresponding position in 'startdate' and 'enddate' vectors. Thus an event id(1) has the start date in startdate(i) and end date in enddate(i).
The program takes a value from the 'id_unique' vector and for each matched value found in 'id', it draws a line in the plot, denoting the beginning and ending time of the event.
For example, suppose 55 is an id-value from vector 'id_unique' and we have this value 1000 times in id. So for 55 a plot is created depicting 1000 separate lines with a marker 'o' at the beginning of the event, a marker 'o' at the end of the event and a line connecting both markers.
Please look at the attached plot that is generated from this code block. If id_unique has 70 values, 70 such plots will be created from this code. In the image, many lines are too small because of small difference between start and end date, so the markers overlap each other and look like a single point.
Now the problem comes when for an id-value in 'id_unique', we have a lot of instances of it in the 'id' vector. When the program plots individual lines unto 100 it works quite fast, but after plotting 300 lines in the same figure the program gets slow. When the program plots 1000 lines in the same figure, it takes about 5-7 seconds for each line. So it takes many hours to generate a plot with many lines.
Is there a way to improve my code to make these plot generation faster.



