This is a matlab problem from the book \"MATLAB: a practical introduction to pra
ID: 3800674 • Letter: T
Question
This is a matlab problem from the book "MATLAB: a practical introduction to pragramming and problem solving (4 ed.)".
I'm beyond tired and I'm not even really sure what this problem wants me to do.
" A file "potfilenames.dat" stores potential file names, one per line. The names do not have any extension. Write a script that will print the names of the valid files, once the extension ".dat" has been added. "Valid" means that the file exists in the Current Directory, so it could be opened for reading. The script will also print how many of the file names were valid."
Explanation / Answer
Matlab code:
clear all;
clc;
fid = fopen('potfilenames.dat');
tline = fgets(fid);
valid = 0;
while ischar(tline)
fname = tline(1:size(tline,2) - 1);
f = strcat(fname,'.dat');
if exist(f, 'file') == 2
valid = valid + 1;
fprintf('file %s does exist ',f);
else
fprintf('file %s does not exists ',f);
end
tline = fgets(fid);
end
fclose(fid);
fprintf('Total %d files are valid ',valid);
potfilenames.dat
file1
file2
file3
file4
Sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.