GPS Time conversion from uint8 to decimal

조회 수: 5 (최근 30일)
Kelvin Wong
Kelvin Wong 2016년 7월 29일
댓글: Kelvin Wong 2016년 7월 30일
I have the following GPS time in decimal as follows [65 31 163 67 100 90 28 172] and the data type is uint8. I need to write a matlab function as a block in Simulink to convert the time to the value 518352.848. Currently, the only way is to convert the data to hex and then back to decimal. As simulink does not accept char type, how can i do this conversion without going through the hex conversion?

답변 (2개)

James Tursa
James Tursa 2016년 7월 29일
Can you use either of these?
>> u = uint8([65 31 163 67 100 90 28 172])
u =
65 31 163 67 100 90 28 172
>> typecast(u(8:-1:1),'double')
ans =
5.183528480000000e+005
>> swapbytes(typecast(u,'double'))
ans =
5.183528480000000e+005
  댓글 수: 1
Kelvin Wong
Kelvin Wong 2016년 7월 29일
Both solutions works fine on matlab command window. However, when i import this code to a matlab function in Simulink. it gave me an error
The simulink model i created is shown below.

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2016년 7월 29일
Use the same kind of steps that I showed you in http://www.mathworks.com/matlabcentral/answers/297415-convert-decimal-to-ieee-754-32-bit-single-precision-floating-point#answer_229990 -- in particular, make sure you initialize the output to be a scalar so that the output of the typecast returns a scalar.
  댓글 수: 2
Kelvin Wong
Kelvin Wong 2016년 7월 30일
I use the same kind of steps that you mentioned earlier and i got the same phenomenon again. The steps work well on the command window but when i put it as a function in the simulink block, i got this error.
What do you mean by initializing the output to be a scalar? Where should i do this in my simulink model? Thanks in advance for the help.
Kelvin Wong
Kelvin Wong 2016년 7월 30일
I've managed to solve this problem by modifying the code that you provided earlier to this.
function y = fcn(~)
%#codegen
GPS = uint8([65 31 163 67 100 90 28 172]);
GPS = fliplr(GPS);
y = zeros(1,1, 'double');
y = typecast(GPS,'double');

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Unit Conversions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by