1
votes

I am plotting an EEG time series in MATLAB. The vector is 4097*1. The duration of recorded signal is 23 seconds. The MATLAB plot function is plotting the signal with amplitude on y-axis and number of sample on x-axis. However, I need the time on x-axis with scale multiple of 5 seconds. The figure is shown with time on x-axis (Referred from a paper). I tried following code using 'xticks', but the x-axis remained same.

hFig = figure;
hAx = gca;
ts = 0:length(d); % the data time series
stairs(ts(2:end), d, 'LineWidth', 2);
xticks ([0 5 10 15 20 25 30])
hAx.XLabel.String = 'Time (Seconds)';

enter image description here

I appreciate your support. Thank you.

1
When you use the xticks function, what do you get?Sardar Usama
How about set(gca,'XTick',0:5:30) ?Sardar Usama
It is not just a matter of selecting the x-ticks, the scaling of the axis is incorrect. Perhaps what @SardarUsama said can be added after executing my solution below, to give exact ticks marks as OP wants.crazyGamer
Yes, @SardarUsama, xticks ([0 5...]) is to set the x-axis tick values, but I was missing the appropriate scaling of the axis, as suggested by crazyGamer. Thank you all.Dattaprasad

1 Answers

1
votes

What you intend to do is to scale your X-axis correctly. Try the following:

hFig = figure;
hAx = gca;
ts = 0:length(d)-1; % the X axis of data time series
% Scale the X-axis to 23 seconds
ts = ts * (23 / length(d));
stairs(ts, d, 'LineWidth', 2);
hAx.XLabel.String = 'Time (Seconds)';

Right now, your X axis is simple a list of integers equal to the number of samples. Scaling them correctly as above should give you the realistic X axis.