Need help with Matlab - I get an error

'A Function that takes a number and return it with the desired number of decimal places';
for i = 0; i < DECIMAL_PLACES;
value = round((rawData(r,c)*100)/(100));
end

1 comentario

Walter Roberson
Walter Roberson el 29 de Sept. de 2016
This is not actually possible with binary floating point; decimal numbers are infinite repeating binary expansions, the same way that 1/7th is an infinite repeating expansion in decimal.

Iniciar sesión para comentar.

Respuestas (2)

Stephen23
Stephen23 el 29 de Sept. de 2016
Editada: Stephen23 el 29 de Sept. de 2016
@Mitch Byatt: you do not need a loop. Don't try to put lots of loops in everything and make code more complicated than it needs to be.
>> fun = @(val,dp) round(val*10^dp)/10^dp;
>> fun(0.123456,3)
ans =
0.123
Jan
Jan el 29 de Sept. de 2016

0 votos

You forgot to mention the error message.
The function definition is missing (the line starting with "function"). Perhaps "DECIMAL_PLACES" is undefined.
You run a loop over "i" but use the potentially undefined indices r and c inside the loop.
The round() is misplaced. It should be applied to "rawData*100" only, not to "rawData*100/100" of course.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 29 de Sept. de 2016

Respondida:

Jan
el 29 de Sept. de 2016

Community Treasure Hunt

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

Start Hunting!

Translated by