This is a matlab problem from the book MATLAB a practical in

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.\"

Solution

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:

This is a matlab problem from the book \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site