MATLAB Answers

Time axis as x axis

조회 수: 58(최근 30일)
Mohamed Jamal
Mohamed Jamal 2020년 7월 15일
편집: Steven Lord 2020년 7월 16일
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!


Paresh yeole
Paresh yeole 2020년 7월 15일
편집: Paresh yeole 2020년 7월 15일
When you do
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
where t can have any step size as long as the lenght of both vectors is same.

Adam Danz
Adam Danz 2020년 7월 15일
편집: Adam Danz 2020년 7월 16일
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 2020년 7월 16일
편집: Steven Lord 2020년 7월 16일
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];
plot(1:length(signal), signal)


Community Treasure Hunt

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

Start Hunting!

Translated by