Plot time of audio file in milliseconds

Hi there,
I have imported a .wav file into MATLAB, I am plotting this audio file in amplitude/time but I need it to be displayed in milliseconds rather than seconds.
The .wav file is configured in the workspace as data and fs.
The code I am using to plot the file in time is :
>> t=[0:length(data)-1] / fs;
>> plot(t, data);
Many thanks in advance for your time.

 Accepted Answer

Try this:
plot(t*1E+3, data);
That converts seconds to milliseconds. The plot otherwise remains the same.

4 Comments

That's great..thanks for such a quick response.
Is there any chance you could also please tell me how to stop the x-axis displaying every value in exponential (10^) form?
For example I would like 10,000 20,000.
I tried ax.XAxis.Exponent = 0 ..but it was unsuccessful.
As always, my pleasure.
To the best of my knowledge, there is no way to directly change the tick label format. However it is relatively straightforward to create your own tick labels:
xt = get(gca, 'XTick');
xtlbl = regexp(sprintf('%.0f\n', xt), '\n', 'split');
set(gca, 'XTick',xt, 'XTickLabel',xtlbl(1:end-1), 'FontSize',6)
The idea here is to get the tick label values, use sprintf to create your own label format, convert it to a cell array (in the regexp call here), then specify the tick positions with the tick labels so MATLAB knows where to put them. I added a 'FontSize' argument to make the tick labels more readable, although it is not necessary for the rest of the code.
Again this is great and really useful.
Thank-you for sharing your knowledge and for explaining it too, it is much appreciated.
Thank you.
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!