Getting "Subscript indices must either be real positive integers or logicals" error from simple calculation

1 view (last 30 days)
When running my code I get the Subscript indices must either be real positive integers or logicals" error.
After some debugging I found where the issue is generated. I have the following variables:
v = 7.1 (readback shows value 7.100000000000000) LOWERVOLTAGE = 7 (readback shows value 7) STEPSIZE = 0.1 (readback shows value 0.100000000000000)
When I run the following code:
Cur_Row = (((v-LOWERVOLTAGE)*(1/STEPSIZE))+1)
I get this as answer:
Cur_Row = 1.999999999999996 (I expected 2)
After this I use Cur_Row to access a matrix and get the error
When I run the code with STEPSIZE = 0.5 I don't get this issue.
I know that I can use a round function as workaround, but I would like to know why this happens
  2 Comments
Adam
Adam on 2 Jul 2018
You can just do an online search for floating point arithmetic or similar phrases and you will get links like, e.g
to explain. Probably not the best link, but then I'm sure you know how to search the internet just as well as I do to find better ones!

Sign in to comment.

Accepted Answer

Adam
Adam on 3 Jul 2018
You can just do an online search for floating point arithmetic or similar phrases and you will get links like, e.g
to explain. Probably not the best link, but then I'm sure you know how to search the internet just as well as I do to find better ones!

More Answers (0)

Tags

Products


Release

R2016b

Community Treasure Hunt

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

Start Hunting!