Problem with adding integers

조회 수: 1 (최근 30일)
Joel Schelander
Joel Schelander 2021년 4월 21일
답변: Steven Lord 2021년 4월 21일
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
David Hill
David Hill 2021년 4월 21일
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 2021년 4월 21일
How do I do that? I have paus on error activated, when I press "step in" it just ends the run

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2021년 4월 21일
..+(Vehicle9{index(9)})+...
% ^ missing DOUBLE
  댓글 수: 1
Joel Schelander
Joel Schelander 2021년 4월 21일
pew, I was just clumsy, again...
Thank you so much Stephen

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Steven Lord
Steven Lord 2021년 4월 21일
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.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by