There are two arrays consisting of 1440 elements each. (for y-axis)
T_start1= {}; %start time
T_end = {}; %end time
x = 1:1:1440 %nodes (x-axis)
I'm trying to figure out how to combine these two arrays (T_start1 and T_end). For example, the first element of T_start1 is paired with the first element of T_end to get a vertical line.
Instead of having two different plot lines, is it possible to do this? That means the first element paired with the other first element. This go on for the other elements. Is it possible?
Current code:
T_start1= {}; %start time
T_end = {}; %end time
figure(4);
x = 1:1:1440 %nodes
y1 = cell2mat(T_start1);
m = cell2mat(T_end);
plot (x,y1,x,m)

 채택된 답변

Voss
Voss 2022년 4월 23일
T_start1 = {182 444 392 201 155}; %start time
T_end = {728 938 674 638 702}; %end time
ydata = [cell2mat(T_start1); cell2mat(T_end)];
nT = size(ydata,2);
xdata = [1:nT; 1:nT; NaN(1,nT)];
ydata(end+1,:) = NaN;
plot(xdata(:),ydata(:),'.-r','LineWidth',2,'MarkerSize',8);

댓글 수: 3

Marc Daniel
Marc Daniel 2022년 4월 23일
편집: Marc Daniel 2022년 4월 23일
Thank you so much!
A little bit confused on the NaN(1,nT) and ydata(end+1,:) = NaN
I tried running the code without NaN(1,nT) and ydata(end+1,:) = NaN
When I compare both graphs, the one with NaN(1,nT) code, the graphs looks much better.
Can you explain to me how it works and why is it necessary to have it?
You're welcome!
If a point in a line has a NaN as its x-coordinate or y-coordinate, then the point is not plotted. The effect is for the line to be broken at that point, i.e., no segment is drawn connecting that point to either of the adjacent points in the line.
In this case I put a NaN after each pair of points in order to create a single line object that contains all the vertical segments separated from each other.
When you removed the NaNs you would've seen that the top of each vertical segment connects to the bottom of the next one:
T_start1 = {182 444 392 201 155}; %start time
T_end = {728 938 674 638 702}; %end time
ydata = [cell2mat(T_start1); cell2mat(T_end)];
nT = size(ydata,2);
xdata = [1:nT; 1:nT];
plot(xdata(:),ydata(:),'.-r','LineWidth',2,'MarkerSize',8);
With a NaN between the top of each vertical segment and the bottom of the next one, those diagonal segments are not drawn, leaving just the vertical ones.
Thanks again! I really appreciate it. ^

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Line Plots에 대해 자세히 알아보기

제품

릴리스

R2022a

태그

질문:

2022년 4월 23일

댓글:

2022년 4월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by