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

4 vues (au cours des 30 derniers jours)
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!

Réponse acceptée

Titus Edelhofer
Titus Edelhofer le 4 Juin 2019
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

Plus de réponses (1)

Sayyed Ahmad
Sayyed Ahmad le 4 Juin 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
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

Catégories

En savoir plus sur Simulink dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by