Time axis as x axis

39 vues (au cours des 30 derniers jours)
Mohamed Jamal
Mohamed Jamal le 15 Juil 2020
Modifié(e) : Steven Lord le 16 Juil 2020
Hi guys, I have signal which it's a vector like
signal=[0.4 0.6 0.8 -0.8 -0.9 0.7];
t=1:length(signal); % in sec
I want to plot the time as x axis, so what Im doing is plot(signal) ; is that correct what I get is in my plot y as related to time? I mean the x axis that I get from plot(signal) is the time in seconds? if not , how can I plot my signal(y axis) in relation with time in sec(time is x axis ) ?
In addition, once we do just plot(signal), if the x axis isn't time so what does x axis represent in that case?
thanks alot for any assistance!

Réponses (3)

Paresh yeole
Paresh yeole le 15 Juil 2020
Modifié(e) : Paresh yeole le 15 Juil 2020
When you do
plot(signal)
Matlab plots the signal values for each data point. But if your x-axis do not vary in the steps of 1, then its better to use
plot(t,signal)
where t can have any step size as long as the lenght of both vectors is same.

Adam Danz
Adam Danz le 15 Juil 2020
Modifié(e) : Adam Danz le 16 Juil 2020
Assuming you've got a vector of time stamps the same size as the signal vector, convert the time stamps to datetime values and then plot,
plot(dateTimeValues, signal)
The x axis will be in datetime (or duration if your x data are duration units).
If you don't have a vector of time stamps or durations, it would be easy enough to create.

Steven Lord
Steven Lord le 16 Juil 2020
Modifié(e) : Steven Lord le 16 Juil 2020
The documentation page for the plot function states, in part:
"plot(Y) creates a 2-D line plot of the data in Y versus the index of each value.
  • If Y is a vector, then the x-axis scale ranges from 1 to length(Y).
  • If Y is a matrix, then the plot function plots the columns of Y versus their row number. The x-axis scale ranges from 1 to the number of rows in Y.
  • If Y is complex, then the plot function plots the imaginary part of Y versus the real part of Y, such that plot(Y) is equivalent to plot(real(Y),imag(Y))."
As stated, your signal variable is a real vector so "the x-axis scale ranges from 1 to length(Y)." The two plots created by the code below should be the same.
signal=[0.4 0.6 0.8 -0.8 -0.9 0.7];
figure
plot(signal)
figure
plot(1:length(signal), signal)

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by