How do I create a matrix from a file from reading the first row and then use that first number to control a "for" loop to build

1 次查看(过去 30 天)
Below is my assignment that I need help on.
" Read the data file, dragCoef06.txt, into a matrix. The first column will contain the year and the second column contains the drag coefficient for vehicles. Write a MATLAB script file that uses the velocity, horizontal force, and air density given above and the drag coefficient read from the data file to compute the frontal area for all the vehicles. Display and print title and column headers over the numbers of a three column matrix with the year, drag coefficient and frontal area of each vehicle. Note: you will need to add the frontal area as the third column of the original input data matrix. Sort the output table by the year column for output. Use fscanf() to read the data file. Read the first number (the number of cars in this file) then use it to control the for loop to build a matrix ONE row at time in the for loop saving one number at a time into the first and second columns of each car row. Use fprintf() title and column headers over the numbers of a three column table of the information in the matrix. Print the table ONE row at a time in a for loop. At the end of each line, mark each frontal area greater than the average frontal area with an asterisk *. Print a legion after the table. "
I have written the code:
clc, clear all
format short
% ***** CONSTANT *****
FILENAME = 'dragCoef06.txt';
HORIZONTAL_FORCE = 350; % Newtons
AIR_DENSITY = 1.225; % kg/m^3
VELOCITY = 25; % m/s
NUM_CARS = 10;
NUM_COLS = 2;
% does file exist?
if ~exist( FILENAME, 'file' )
disp ( 'File not avaliable' )
else
% file exist contiune
% ***** INPUT *****
% read year and drag coefficient from file
fileID = fopen ( FILENAME );
% ***** COMPUTE *****
% compute the frontal area all cars
for r = 1:NUM_CARS
for c = 1:NUM_COLS
dragMatrix06(r,c) = fscanf( fileID, '%f', 1 );
end
end
frontalArea = ( 2.*HORIZONTAL_FORCE )./ ( AIR_DENSITY.*VELOCITY^2.*dragMatrix06 );
% add frontal area as third column of matrix
newMatrix = [ dragMatrix06, frontalArea ];
% ***** OUTPUT *****
% print table using fprintf()
fprintf ( '%21s \n', 'All Cars' )
fprintf ( '%9s', 'Year' )
fprintf ( '%10s', 'Drag' )
fprintf ( '%11s \n', 'Frontal' )
fprintf ( '%19s', 'Coef' )
fprintf ( '%13s \n', 'Area(m^2)' )
fprintf ( '%9.0f %9.2f %9.4f \n', [newMatrix]' )
fclose ( fileID );
end
this outputs the following:
All Cars
Year Drag Frontal
Coef Area(m^2)
10 2015.00 0.0914
0 0.24 2016.0000
4 0.00 0.2700
2016 3.39 0.0005
0 2016.00 2.5397
0 0.39 2017.0000
2 0.00 0.2200
2017 4.16 0.0005
0 2018.00 3.9752
0 0.26 2018.0000
4 0.00 0.3300
2018 2.77 0.0005
0 2019.00 2.0317
0 >>
any help?
I was given a file...
10
2015 0.24
2016 0.27
2016 0.36
2016 0.39
2017 0.22
2017 0.23
2018 0.26
2018 0.33
2018 0.45
2019 0.28
I need it to output a table that looks like this. Thank you!
  3 个评论

请先登录,再进行评论。

回答(2 个)

MOHD AQUIB
MOHD AQUIB 2020-3-4
Hi,
May be you have to alter your code a little bit.
This can be one of its version:
clc, clear all
format short
% ***** CONSTANT *****
FILENAME = 'dragCoef06.txt';
HORIZONTAL_FORCE = 350; % Newtons
AIR_DENSITY = 1.225; % kg/m^3
VELOCITY = 25; % m/s
NUM_CARS = 10;
NUM_COLS = 2;
% does file exist?
if ~exist( FILENAME, 'file' )
disp ( 'File not avaliable' )
else
% file exist contiune
% ***** INPUT *****
% read year and drag coefficient from file
fileID = fopen ( FILENAME );
% ***** COMPUTE *****
% compute the frontal area all cars
dragtemp=fscanf(fileID,'%f');
k=1;
for r = 1:NUM_CARS
for c = 1:NUM_COLS
k=k+1;
dragMatrix06(r,c) = dragtemp(k);
end
end
frontalArea = ( 2.*HORIZONTAL_FORCE )./ ( AIR_DENSITY.*VELOCITY^2.*dragMatrix06 );
% add frontal area as third column of matrix
newMatrix = [ dragMatrix06, frontalArea ];
newMatrix(:,3)=[];
% ***** OUTPUT *****
% print table using fprintf()
fprintf ( '%21s \n', 'All Cars' )
fprintf ( '%9s', 'Year' )
fprintf ( '%10s', 'Drag' )
fprintf ( '%11s \n', 'Frontal' )
fprintf ( '%19s', 'Coef' )
fprintf ( '%13s \n', 'Area(m^2)' )
fprintf ( '%9.0f %9.2f %9.4f \n', [newMatrix]' )
fclose ( fileID );
end

BobH
BobH 2020-3-4
MOHD AQUIB is on the right track, reading one data point first before the loop, but left out the part within the loop that continues to pull pairs of data points from the file, as you had in your original question.
So after the open, but just before your loop, add
NUM_CARS = fscanf( fileID, '%f', 1 );
The second problem you have was also caught by MOHD AQUIB, your newmatrix has four columns but your final fprintf only has three data items per line. This gives the staggered look to your output data. Try
newMatrix = [ dragMatrix06, frontalArea ];
newMatrix(:,3)=[]; % MOHD eliminates the unwanted column before print
Or instead, never bring the unwanted column in
newMatrix = [ dragMatrix06, frontalArea(:,2) ];
  5 个评论
BobH
BobH 2020-3-5
Of course. After you compute the mean, then create a new loop, using r and NUM_CARS again, and examine each frontalArea element, fprintf most of your row but don't use the \n yet. If greater than mean then print an asterisk. Then have a separate fprintf statement just for the newline \n

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by