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)

 Réponse acceptée

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 commentaires

Marc Daniel
Marc Daniel le 23 Avr 2022
Modifié(e) : Marc Daniel le 23 Avr 2022
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. ^

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Line Plots dans Centre d'aide et File Exchange

Produits

Version

R2022a

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by