MATLAB Answers

How can I round double-values down in a Matlab function block in Simulink?

조회 수: 12(최근 30일)
I want to generate a timestamp from the unix time, which is delivered by an external C-function in a MATLAB function block in a simulink project. I'm trying to round the double values for hours, minutes and seconds to the integer in front of the decimal point. However, the function always rounds to the nearest integer. So my hours round up after 30 minutes and my minutes round up after 30 seconds. I tried the floor() function already, but it leaves the value unaltered. I tried to set up the fimath()-function in the model explorer and at the beginning of the script, but I guess it only changes fixed point values? (duh)
I have no idea what else to try. Where is my mistake?
leftover_seconds = leftover_seconds - (sum_days + days - 1)*24*3600; %here I calculate the elapsed seconds of the current day, which are correct
hours = leftover_seconds/3600;
minutes = (leftover_seconds - hours*3600)/60;
seconds = leftover_seconds - ((hours*3600) + (minutes*60));
I deleted the floor() functions in front of each line, because it didn't do anything.
I appreciate any tips. Thank you!

  댓글 수: 0

로그인 to comment.

채택된 답변

Titus Edelhofer
Titus Edelhofer 4 Jun 2019
strange: floor should indeed be your friend. The only thing I can imagine is that leftover_seconds is not a double but an integer. Therefore the division is done as integer (and rounds ...):
leftover_seconds = uint16(7199);
hours = leftover_seconds/3600
This leads in MATLAB to
hours = leftover_seconds/3600
hours =
This doesn't happen if you do this:
leftover_seconds = double(leftover_seconds);
hours = floor(leftover_seconds/3600)
hours =

  댓글 수: 1

로그인 to comment.

More Answers (1)

Sayyed Ahmad
Sayyed Ahmad 4 Jun 2019
I would programming like this:
x=strsplit(datestr(now,'YYYY-mm-DD-HH-MM-ss'),'-') % instead of now you can use your time from external C++ program
instead of

  댓글 수: 0

로그인 to comment.

이 질문에 답변하려면 로그인을(를) 수행하십시오.




Translated by