Create a file which stores on each line a letter, a space, and a real number. For example, it might look like this:
belongs to book: MATLAB: A Practical Introduction to Programming and Problem Solving|Stormy Attaway|Fourth Edition| Chapter number:9| Question number:21.9
All Answers
total answers (1)
Ch9Ex21.m
% use textscan to read in from a file in the
% format letter number and sum the numbers
fid = fopen('letnum.dat');
if fid == -1
disp('File open not successful')
else
%Read in data from file
C = textscan(fid,'%c %f');
total = sum(C{2}); %Sum the numbers in the file
fprintf('The sum of the numbers is %.2f\n',total)
%Error-check file close
closeresult = fclose(fid);
if closeresult == 0
disp('File close successful')
else
disp('File close not successful')
end
end
need an explanation for this answer? contact us directly to get an explanation for this answer