Error using bar (line 159) X must be same length as Y. Error in Lab3_Q1234 (line 60) bar(x, y);

3 views (last 30 days)
% Author Name: Foredul Islam
% Student ID: U0000008501
%Question_1.Since the data logs do not contain samples with a fixed period, the samples will be weighted based onthe amount
%of time spent in each sample. To facilitate this, create a new array in from the input file such that has in each row:
% • the time stamp from the data log file
% • the elapsed time since the previous sample, which is time[n]-time[n-1]
% • the averaged continuous readings, which is (value[n]+value[n-1])/2,
%or the unmodified discrete value (just the Gear Position)Your new array will have one fewer row than the input file
%since the log file’s rows 1 and 2 are used to calculate the first row in the new array, rows 2 and 3 are used for the
%second row, and so on. This array must be returned by the function, but it does not need to be output or captured as a
%deliverable. (Hint: when calling the function, end the line with a ‘;’ so 100,000’s of rows doesn’t print to the command
%window.)
f=readmatrix("datalog1.csv");
f(1:10,:)
len=size(f,1)
N=zeros(len-1,8);
N(:,[1 6])=f(2:end,[1,5]);
N(:,2)=f(2:end,1)-f(1:(len-1),1);
N(:, [3 4 5 7 8])=(f(2:len, [2 3 4 6 7])+f(1:(len-1), [2 3 4 6 7]))/2;
N(1:10,:)
%Question 2. Using the weighted data from step 1 to calculate the overall weighted average for Fuel Economy.
%This result must be printed to the Command Window with an appropriate message.
data = N(:,2);
f_economy = N(:,5);
avg_economy = sum(f_economy.*data)/sum(data)
%Question3. Since the value in step 2 accounts for time spent sitting still at stop lights and stop signs, it may be
%interesting to see what the vehicle is capable of if traffic is not an issue. Calculate the weighted average for Fuel
%Economy excluding all samples with a Vehicle Speed of 0 mph. This result must be printed to the Command Window with
%an appropriate message.
weight = N(N(:,8)>0,:);
weight_data = weight(:,2);
weight_f_economy = weight(:,5);
weght_avg_economy = sum(weight_f_economy.*weight_data)/sum(weight_data)
% Question_4
maxim_sp = max(N(:,8));
y = zeros(1, ceil(maxim_sp/5));
for i = [0:5:ceil(maxim_sp)]
n = N( (N(:,8)<(i+5)) & (i<=N(:,8)), :);
data = n(:,2);
fuel_economy = n(:,5);
avg_economy = sum(fuel_economy.*data)/sum(data);
x(floor(i/5)+1) = i;
y(floor(i/5)+1) = avg_economy;
end
bar(x, y);
title("weighted Average Fuel Economy in 5 MPH Increments")
xlabel("Vehicle Speed (MPH)")
ylabel("Weighted Average Fuel Economy")

Answers (1)

Walter Roberson
Walter Roberson on 24 Feb 2020
You preallocate y but not x. If you got the preallocation wrong then the two can end up different sizes. And you did get the preallocation wrong.
Suppose maxim_sp is 5. ceil(5/5) is 1 so you would preallocate y as zeros(1,1). Then the loop is 0:5:ceil(5) which is 0:5:5 which contains 0 and 5 which is length 2, not 1.
You need to make sure that the size allocated for x and y are the same and match the number of loop iterations.
Hint:
ivals = 0:5:ceil(maxim_sp)
numi = length(ivals)
y = zeros(1,numi)

Categories

Find more on General Applications in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!