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

4 次查看(过去 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!

采纳的回答

Titus Edelhofer
Titus Edelhofer 2019-6-4
Hi,
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 =
uint16
2
This doesn't happen if you do this:
leftover_seconds = double(leftover_seconds);
hours = floor(leftover_seconds/3600)
hours =
1
Regards,
Titus

更多回答(1 个)

Sayyed Ahmad
Sayyed Ahmad 2019-6-4
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
year=num2str(x{1})
mount=num2str(x{2})
day=num2str(x{3})
hour=num2str(x{4})
minute=num2str(x{5})
secound=num2str(x{6})
instead of

类别

Help CenterFile Exchange 中查找有关 Simulink 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by