How to add trip_sum to table without error?

1 view (last 30 days)
Hey so my table populates without tripsum but when i go to add tripsum i get error stating- Error using ()
The number of table variables in an assignment must match.
For each car i count battery usage in 5 min increments and want to sum usage for each trip, then move to next trip. I do get correct value for my last trip(j value) in workspace so unsure why it wont attribute. thanks
for j = 1:tripsedan
trip_num = j;
duration= round(tripdistances(j)/fivemindist);
endtriptime= round(departuretime + duration);
nextdeparturetime= round(endtriptime + randi([6 48 ]));
trip_sum = 0;
for k=departuretime:endtriptime
x=temperaturesinterval(k);
% check potential trip usage
usage(k)= (p1*x^2 + p2*x + p3) * fivemindist;
trip_sum = trip_sum + usage(k);
% check state of charge after each trip
SOCLevel=(((SOCLevel/100)*Sedan_Battery(btType)-trip_sum)/100);
% added=0;
% if SOCLevel<= 20
% % charge car fully or partially
% for l = endTripTime:nextdeparturetime-1
% demand(l) = charger(chType)/12;
% added= added +demand(l);
% end
% SOCLevel= ((SOCLevel/100)*Sedan_Battery + added)/Sedan_Battery*100;
% end
end
% trip_table(j,:)= table(trip_num, departuretime, endtriptime, tripdistances(j), ...
% 'VariableNames', {'TripNum', 'DepartureTime', 'EndTripTime', 'TripDistance'});
trip_table(j,:)= table(trip_num, departuretime, endtriptime, tripdistances(j), trip_sum, ...
'VariableNames', {'TripNum', 'DepartureTime', 'EndTripTime', 'TripDistance', 'TripSum'});
departuretime= round(nextdeparturetime);
end
end
end

Answers (1)

Walter Roberson
Walter Roberson on 5 Feb 2023
You have an existing trip_table variable that was built with a different number of variables.
I suggest you build your table once, and then make assignments along the lines of
trip_table(end+1,:) = {trip_num, departuretime, endtriptime, tripdistances(j), trip_sum};
When you assign a cell array into an existing table, the contents of the cells are matched to the corresponding variable by position.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!