convert milliseconds (ms) to seconds (s)

Hello,
we have an array with stimuli onset times in milliseconds,
ms = [1600901 1970185 2340488 2710467 3080481 3449869]
and we want to convert those values to seconds with a specific format, that would be, 1600901/1000 = 1600.901.
But when I do that in MATLAB I get this result:
s = ms/1000
s =
1.0e+03 *
1.6009 1.9702 2.3405 2.7105 3.0805 3.4499
Is there a way to obtain this insead:
s = [1600.901 1970.185 2340.488 2710.467 3080.481 3449.869]
Thank you

 Accepted Answer

Stephen23
Stephen23 on 26 Jul 2019
Edited: Stephen23 on 26 Jul 2019
You can change the format of how numbers are displayed, e.g. "shortG"
>> format shortG
>> ms/1000
ans =
1600.9 1970.2 2340.5 2710.5 3080.5 3449.9
Or "longG":
>> format longG
>> ms/1000
ans =
1600.901 1970.185 2340.488 2710.467 3080.481 3449.869
The default is "short":
>> format short
>> ms/1000
ans =
1.0e+03 *
1.6009 1.9702 2.3405 2.7105 3.0805 3.4499
Note that none of these make any difference to the numeric values stored in your array. Do not confuse how numbers are displayed with what values are stored in a variable. These are two quite different things.

4 Comments

Hi Stephen,
Thank you for your response.
I would like to change the way the values are stored. That means if I open the "s" array from the workspace I want to see 1600.901 and not 1.0e+03 * 1.6009.
That's what I am trying to do.
Stephen23
Stephen23 on 26 Jul 2019
Edited: Stephen23 on 26 Jul 2019
"I would like to change the way the values are stored. That means if I open the "s" array from the workspace I want to see 1600.901 and not 1.0e+03 * 1.6009."
Numeric data classes do not store any format information.
You can change the format for the Command Window and for the Variable Viewer and anything else that has a customizable display format, but using standard binary floating point numbers what you ask for is impossible because they do NOT store any formatting information whatsoever.
You are indeed confusing the ways the number are stored and display.
Matlab store 1600.901 neither as 1600.901 or 1e3 * 1.6009. The way it is stored is actually irrelevant but it is stored as a binary fraction (1.1001000000111001101010011111101111100111011011001001b * 2 ^ 10, resolving to ~= 1.5633798828125 * 2^10) and there's nothing you can change about that. The actual decimal value stored in memory is 1600.9010000000000673026079312.
You are asking to change the way matlab displays the number to you, and for that you should use format shortg or format longg as answered by Stephen.
Hi Guillaume and Stephen,
Thank you both for your asnwer and for taking the time to explain in such a clear way how floating numbers are displayed/stored.

Sign in to comment.

More Answers (1)

madhan ravi
madhan ravi on 26 Jul 2019
Edited: madhan ravi on 26 Jul 2019
format longg
ms / 1000
doc format

3 Comments

Hi madhan,
Thank you for your response but I think what you are suggesting changes the way the numbers are displayed and not the way they are stored.
Thank you madhan for your answer.

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!