Write a MATLAB script You have to create at least 3 .dat files in this problem.
ID: 3797989 • Letter: W
Question
Write a MATLAB script
You have to create at least 3 .dat files in this problem.
A set of data files named “exfile1.dat”, “exfile2.dat”, etc. has been created by a series of experiments. It is not known exactly how many there are, but the files are numbered sequentially with integers beginning with 1. The files all store combinations of numbers and characters, and are not in the same format. Write a script that will count how many lines total are in the files. Note that you do not have to process the data in the files in any way; just count the number of lines.
Explanation / Answer
Code:
prefix = 'abc';
i=1;
suffix = '.m';
numLines = 0;
while (exist (strcat(strcat(prefix, num2str(i)),suffix), 'file')==2)
fid = fopen(strcat(strcat(prefix, num2str(i)),suffix), 'rb');
%# Get file size.
fseek(fid, 0, 'eof');
fileSize = ftell(fid);
frewind(fid);
%# Read the whole file.
data = fread(fid, fileSize, 'uint8');
%# Count number of line-feeds and increase by one.
numLines =numLines + sum(data == 10) + 1;
i=i+1;
fclose(fid);
end
printf("%d number of lines present. ", numLines);
The script is very very simple. You can understand everything! change prefix and substitute if requited.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.