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
Afficher commentaires plus anciens
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!

Réponses (2)
MOHD AQUIB
le 4 Mar 2020
2 votes
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
2 commentaires
engrCE1
le 4 Mar 2020
MOHD AQUIB
le 5 Mar 2020
BobH has commented about the part that I have missed, I hope including that part in the code that I have shared will serve your purpose.
BobH
le 4 Mar 2020
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 commentaires
BobH
le 4 Mar 2020
To print asterisks, you will have an additional field in your final fprintf, %c and give it a number that represents a blank (32) or an asterisk (42). You can confirm the proper numbers in any ASCII table.
First, consider changing your frontalArea computation to use only the interesting column of input data; as it stands, your first column of frontalArea is the computation done on the year value, which is nonsense. The interesting input column is dragMatrix06(:,2)
Compute the mean
Define an array (for ex, named 'ast') to hold blanks or asterisks, and assign the initial value of 32 (blank) to all
Change those elements of the array whose area is greater than the mean. "frontalArea > faMean" returns a logical array (trues or falses) which can be used to pick locations in the array.
% for those locations where area > mean, replace blank with '*'
ast( frontalArea > faMean ) = 42; % 42 is ASCII asterisk
Append 'ast' to your newmatrix
Add a %c to the final fprintf statement
Thomas Smith
le 5 Mar 2020
I'm trying this for myself, but I don't really understand how to do the part of appending an asterisk, can you share the full code for how to do this?
BobH
le 5 Mar 2020
% Only compute using second column of dragMatrix06; frontalArea will be a single column
frontalArea = ( 2.*HORIZONTAL_FORCE )./ ( AIR_DENSITY.*VELOCITY^2.*dragMatrix06(:,2) );
% compute mean
faMean = mean(frontalArea);
% define an array of the correct size, and init with blanks
% NOTE using numbers not strings, to keep fprintf simple
ast = zeros( length(frontalArea), 1 ) + 32; % 32 is ASCII blank
% for those locations where area > mean, replace blank with '*'
ast( frontalArea > faMean ) = 42; % 42 is ASCII asterisk
% add frontal area as third column of matrix
newMatrix = [ dragMatrix06, frontalArea, ast ];
the extra character to the screen via " %c"
fprintf ( '%9.0f %9.2f %9.4f %c\n', [newMatrix]' )
engrCE1
le 5 Mar 2020
BobH
le 5 Mar 2020
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
Catégories
En savoir plus sur Mathematics and Optimization dans Centre d'aide et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!