Clear Filters
Clear Filters

Problem with adding integers

2 views (last 30 days)
Joel Schelander
Joel Schelander on 21 Apr 2021
Answered: Steven Lord on 21 Apr 2021
I am running 36 functions. At the 17th I get an error at this part of a loop:
INCREASE2{oz}=max(HH2+double(Vehicle1{index(1)})+double(Vehicle2{index(2)})+double(Vehicle3{index(3)})+double(Vehicle4{index(4)})+...
double(Vehicle5{index(5)})+double(Vehicle6{index(6)})+double(Vehicle7{index(7)})+double(Vehicle8{index(8)})+(Vehicle9{index(9)})+...
double(Vehicle10{index(10)})+double(Vehicle11{index(11)})+double(Vehicle12{index(12)})+double(Vehicle13{index(13)})+double(Vehicle14{index(14)})+...
double(Vehicle15{index(15)})+double(Vehicle16{index(16)})+double(Vehicle17{index(17)}))./max(HH2);
Error using +
Integers can only be combined with integers of the same class, or scalar doubles.
The thing is, running the functions before works fine, the same part in the 16th function looks like this:
INCREASE2{oz}=max(HH2+double(Vehicle1{index(1)})+double(Vehicle2{index(2)})+double(Vehicle3{index(3)})+double(Vehicle4{index(4)})+...
double(Vehicle5{index(5)})+double(Vehicle6{index(6)})+double(Vehicle7{index(7)})+double(Vehicle8{index(8)})+double(Vehicle9{index(9)})+...
double(Vehicle10{index(10)})+double(Vehicle11{index(11)})+double(Vehicle12{index(12)})+double(Vehicle13{index(13)})+double(Vehicle14{index(14)})+...
double(Vehicle15{index(15)})+double(Vehicle16{index(16)}))./max(HH2);
All Vehicle(nr) are 1x108 cells containing 108 52560zx1 int8, index is a 1x17 double, HH2 is a 52560x1 double. I do not understand whats wrong. Any ideas?
  2 Comments
David Hill
David Hill on 21 Apr 2021
I would stop the application on that line and then run each term of your equation until you find your problem.
Joel Schelander
Joel Schelander on 21 Apr 2021
How do I do that? I have paus on error activated, when I press "step in" it just ends the run

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 21 Apr 2021
..+(Vehicle9{index(9)})+...
% ^ missing DOUBLE
  1 Comment
Joel Schelander
Joel Schelander on 21 Apr 2021
pew, I was just clumsy, again...
Thank you so much Stephen

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 21 Apr 2021
Set an error breakpoint and run your code. When you reach the line that throws the error, check that what you believe to be true about the classes of the variables used on that line is actually true.
As an aside, this:
INCREASE2{oz}=max(HH2+double(Vehicle1{index(1)})+double(Vehicle2{index(2)})+double(Vehicle3{index(3)})+double(Vehicle4{index(4)})+...
double(Vehicle5{index(5)})+double(Vehicle6{index(6)})+double(Vehicle7{index(7)})+double(Vehicle8{index(8)})+(Vehicle9{index(9)})+...
double(Vehicle10{index(10)})+double(Vehicle11{index(11)})+double(Vehicle12{index(12)})+double(Vehicle13{index(13)})+double(Vehicle14{index(14)})+...
double(Vehicle15{index(15)})+double(Vehicle16{index(16)})+double(Vehicle17{index(17)}))./max(HH2);
smells like week-old fish to me. [Do you really want to type this out for say 50, 100, 1000, or more vehicles?]
The general consensus is that using variables named Vehicle1, Vehicle2, Vehicle3, ... is strongly discouraged as an anti-pattern. "Repeating yourself" or "Copy and paste programming" seem the most applicable. I'd probably use an N-dimensional array or maybe a 2-dimensional cell array instead.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!