Azzera filtri
Azzera filtri

Solving a "For" loop problem with imported data?

1 visualizzazione (ultimi 30 giorni)
Miranda Moore
Miranda Moore il 29 Nov 2016
Risposto: Jefferson Lopez il 5 Dic 2016
I have copied and pasted the problem itself below.
Every summer, many people travel by road to various locations across the country. With low fuel prices this summer, there will be lots of travel. The purpose of this program is to determine the total miles of travel and also compute the cost of travel using various travel route options. The excel sheet named project3.xlsx has data for this work. The table represents 55 families travel plans. Each family is identified by a family ID. Each travel plan consists of 10 road segments whose distances are given in miles. Column 11 is the average speed of the family and column 12 is the fuel efficiency of the family’s vehicle. For example, for family 1, the average speed is 65mph and they have a vehicle that does 35 miles per gallon. Write a function called summetravel that uses a for loop to generate the table below. Your table should show all the outputs from 1 to 55. Use $2/gallon as the average fuel cost.
So an example of the data itself is:
FAMILY 1 2 3 4 5 6 7 8 9 10 11 12
1 40 150 70 100 140 202 51 44 12 95 65 35
2 100 121 57 105 66 182 54 87 8 141 75 20
The table that the code is supposed to result in includes the total miles driven, total hours, and predicted fuel cost. So I understand the main concept behind it, assigning each group variables then using different operations such as adding road segments and dividing total distance to find other quantities. I am just unsure of how to do this doing a "for" loop and how to adjust for each variable accessing different data from the excel spreadsheet.
I just have absolutely no idea where to even start. Every code I have tried has given me countless errors and gotten me nowhere closer to a solution. If you could provide an example of the code itself, or maybe even start it, I would really appreciate it. Thanks

Risposte (2)

Miranda Moore
Miranda Moore il 29 Nov 2016
So an example of the data itself is:
FAMILY
1 2 3 4 5 6 7 8 9 10 11 12
1 40 150 70 100 140 202 51 44 12 95 65 35
2 100 121 57 105 66 182 54 87 8 141 75 20

Jefferson Lopez
Jefferson Lopez il 5 Dic 2016
Hey Miranda, I actually think I have the same professor; Baffour? Anyways, I realize that it is due tomorrow so you may not need my response anymore but this is what I did:
function summertravel
T=xlsread('project3data');
fprintf('Family\tTotal Miles Driven\t\tTotal Hours\t\tExpeced Fuel Cost\n')
for f=[2:56]
TMD=sum(T(f,2:11));
TH=TMD/(T(f,12));
EFC=(TMD/(T(f,13)))*2.30;
fprintf('\t%d\t\t%d miles\t\t\t%2.1f hours\t\t$%2.2f\n',f-1,TMD,TH,EFC)
end

Community Treasure Hunt

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

Start Hunting!

Translated by