How to connect the point of the plot on Matlab?

28 次查看(过去 30 天)
So I have the following code, which is supposed to update the P column of matrix x by adding columns 2 and 5 together. Then the code is supposed to add tall rows of column P together. This process will continue for 10 hours by using "for loop"
Now, when I plot by using 'plot function' then I dont see any graph, but when I use scatter then I see graph of discrete time which make sense and the scatter graph is correct.
Now, how do I connect those points together so my graph will look like continuous time.
(Please, do not say you dont need for loop for that because there is a lot of code that goes inside the for loop, I am just trying to give simple idea of my code.
Would you please tell me how to solve this?
for y=1:10
P=BR(:,2) + BR(:,5)
Ptotal=sum(P())
% figure('Name','Load vs Time') %Also, how can I fix this line so that my figure has a name and all points will appear on a single figure
scatter(y,Ptotal,'LineWidth',10)
title('load vs time')
xlabel('Time(s)')
ylabel('load')
hold on
end
hold off
  1 个评论
DGM
DGM 2021-12-4
编辑:DGM 2021-12-4
scatter() is for scattered plots -- markers without lines.
plot() will plot markers and/or lines as specified.
If you're plotting one single point at a time, you're not plotting a series. You're plotting single points. There isn't anything to draw a line between. Each point is its own series. This approach is typically undesired.
When using plot() like that, the reason you see nothing is because you haven't specified a marker style. Each plot object has no marker, but has a solid line between points -- but since there's only one point in each series, there are no lines either. If you specify a marker style, then you'll be in the same scenario you are with the scatter plot: a bunch of unrelated points that can't be connected.
Don't plot anything one point at a time. Collect your data and store it in a vector/array and then plot it when you're done.

请先登录,再进行评论。

回答(1 个)

dpb
dpb 2021-12-4
%figure('Name','Load vs Time')
%Also, how can I fix this line so that my figure has a name and all points will appear on a single figure
What's wrong with title? A figure is just a container for the axes object; it is the axes that has a title.
hF=figure;
hAx=axes;
title('load vs time')
xlabel('Time(s)')
ylabel('load')
hold on
for y=1:10
P=BR(:,2) + BR(:,5)
Ptotal=sum(P())
scatter(y,Ptotal,'LineWidth',10)
end
hold off
Above takes all the invariant stuff outside the loop -- there's no point redrawing the same title and labels every time.
For animation, look instead into using animatedline and addpoints instead. They're designed for that purpose. One can solve the problem other ways, but with the introduction of new features, there's no point in regressing.

类别

Help CenterFile Exchange 中查找有关 Discrete Data Plots 的更多信息

标签

产品


版本

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by