matlab code problem 3 cowboys 12 redskins 2 eagles 14 chargers 1 saints 9 bears
ID: 3746683 • Letter: M
Question
matlab code problem
3 cowboys
12 redskins
2 eagles
14 chargers
1 saints
9 bears
10 oilers
4 cardinals
5 49ers
Problem Statement In the file above, rankings are given for various football teams. Write a script to ask the user for the filename. Then read the ranking for the Eagles and the Cardinals from the file, writing their ranking into a new file. Prompt the user for the filename of this new file. The new file should look like this:
Eagles: 2
Cardinals: 4
However, you may assume that the teams are given in the same order as above. This means that the Eagles are guaranteed to be on line 3 and the Cardinals are guaranteed to be on line 8.
Explanation / Answer
Code:
fid = fopen('input.txt');
%taking filename as input from user
fileName = input('Please enter the file name: ','s');
fileName1=[fileName,'.txt'];
fidw = fopen(fileName1,'w');
%reading the inputfile
tline = fgetl(fid);
%checking for eagles and cardinals
%and writing the data to the output file
while ischar(tline)
line = strsplit(tline,' ');
if(strcmp(line{2},'eagles'))
fprintf(fidw,"Eagles: %d ",line{1});
end
if(strcmp(line{2},'cardinals'))
fprintf(fidw,"Cardinals: %d",line{1});
end
tline = fgetl(fid);
end
fclose(fidw);
fclose(fid);
Output:
Output file:
Eagles: 2
Cardinals: 4
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.