Undefined function or variable using readtable and IF
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi there,
Im not sure why I am getting the error "UNDEFINED FUNCTION OR VARIABLE LANE1" at the end of the code where I am trying to assemble a table. Its obviously not looking at my, if Lanes == n, and I am not sure why. Here is the full code:
clear all
clc
%Percentage of different types of vehicles on roads
PercentageOfCars = 0.85 ;
PercentageOfBuses = 0.05 ;
PercentageOfTrucks = 0.10 ;
%Meters converted into miles
CarLength = 5 * 0.00062137;
BusLength = 10 * 0.00062137;
TruckLength = 15 * 0.00062137;
%Grabbing all data from excel file
DataSet = readtable('HighwayInformation.xls');
%Assinging variables from excel sheet
Mph = DataSet.SpeedLimitInMph;
SectionLength = DataSet.LengthOfTheSection_miles_;
Lanes = DataSet.NumberOfLanes;
BrakingDistanceCars = DataSet.CarBrakingDistance_miles_;
BrakingDistanceBuses = DataSet.BusBrakingDistance_miles_;
BrakingDistanceTrucks = DataSet.TruckBrakingDistance_miles_;
TimeToCompleteLane = (SectionLength ./ Mph); %Time in hours to complete respective section
TotalLengthWithLanes = SectionLength .* Lanes ; %Total length of section in miles
NumberOfVehiclesThatCanFitInLane = floor(SectionLength ./ (((BrakingDistanceCars + CarLength) * PercentageOfCars) + ((BrakingDistanceBuses + BusLength) * PercentageOfBuses) + ((BrakingDistanceTrucks + TruckLength) * PercentageOfTrucks)));
format shortg
PhasesOfVehicles = (1)./(TimeToCompleteLane);
MFV = (PhasesOfVehicles .* NumberOfVehiclesThatCanFitInLane);
%Lane 1 is inside lane
if Lanes == 2
Lane1 = MFV;
Lane2 = MFV * 0.95;
Lane3 = nan;
Lane4 = nan;
Lane5 = nan;
Lane6 = nan;
elseif Lanes == 3
Lane1 = MFV;
Lane2 = MFV * 0.95;
Lane3 = MFV * 0.90;
Lane4 = nan;
Lane5 = nan;
Lane6 = nan;
elseif Lanes == 4
Lane1 = MFV;
Lane2 = MFV * 0.95;
Lane3 = MFV * 0.90;
Lane4 = MFV * 0.85;
Lane5 = nan;
Lane6 = nan;
elseif Lanes == 5
Lane1 = MFV;
Lane2 = MFV * 0.95;
Lane3 = MFV * 0.90;
Lane4 = MFV * 0.85;
Lane5 = MFV * 0.80;
Lane6 = nan;
elseif Lanes == 6
Lane1 = MFV;
Lane2 = MFV * 0.95;
Lane3 = MFV * 0.90;
Lane4 = MFV * 0.85;
Lane5 = MFV * 0.80;
Lane6 = MFV * 0.75;
end
T = table(SectionLength,Mph,Lanes,Lane1,Lane2,Lane3,Lane4,Lane5,Lane6)
The rest of the code is working fine.
댓글 수: 0
답변 (1개)
Adam Danz
2019년 4월 24일
편집: Adam Danz
2019년 4월 24일
As the error message indicates, there is no variable (or function) named 'Lane1' which means "Lanes" does not equal 2,3,4,5, or 6. You could add a last "else" at the end of your conditionals to catch such errors.
else
error('''Lanes'' variable has unexpected value: %.3f', Lanes)
end
Also, if you're using discrete values, consider using a switch case instead.
%Lane 1 is inside lane
switch Lanes
case 2
Lane1 = MFV; %etc...
case 3
Lane1 = MFV;
case 4
Lane1 = MFV;
case 5
Lane1 = MFV;
case 6
Lane1 = MFV;
otherwise
error()
end
댓글 수: 3
Adam Danz
2019년 4월 24일
" Lanes is a 647x1 double supposedly".
The 'if' command operates on a scalar value, not a vector.
If you're testing whether ALL of the values are equal to 2,
if all(lanes==2)
If you're testing that ANY of the values equal 2,
if any(lanes==2)
If you're just interested in the first value of lanes,
if lanes(1)==2
Walter Roberson
2019년 4월 24일
And if you want to process multiple entries in Lanes then you should use logical indexing.
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!