To modify:
The script “fileex” to sum the numbers from the file and create a file in this format first.
Answer to Problem 9.1P
Solution:
The script is saved and executed as follows.
Explanation of Solution
MATLAB Code:
fid = fopen('subjexp.dat');
%Define the instruction to read information from "subjexp.dat" and open them.
if fid == -1
disp('Unsuccessful; The file is not opened')
%Define the instruction to display the unsuccessful result.
else
SUM = 0;
while ~feof(fid)
Number = strtok(fgetl(fid));
SUM = SUM + str2double(Number);
%Define the instruction to separate each line into the number code and %converts to a number before printing.
end
closeresult = fclose(fid);
%Define the instruction to check the file close for success.
if closeresult == 0
disp('successful: File is closed')
else
disp('Unsuccessful: File does not close')
%Define the if-else statement to check the condition for closing the
%file, and display the relative statement.
end
end
fprintf('The total sum of the numbers is: %.2f\n', SUM)
%Define the instruction to create the total sum of the numbers.
Save the MATLAB script with name, chapter_9_54793_9_1P.m in the current folder. Execute the script by typing the string name at the command window to modify the script “fileex” to sum the numbers from the file and create a file in this format first.
Result:
Therefore, the script is saved and executed.
Want to see more full solutions like this?
Chapter 9 Solutions
MATLAB: A Practical Introduction to Programming and Problem Solving
- MATLAB: An Introduction with ApplicationsStatisticsISBN:9781119256830Author:Amos GilatPublisher:John Wiley & Sons IncProbability and Statistics for Engineering and th...StatisticsISBN:9781305251809Author:Jay L. DevorePublisher:Cengage LearningStatistics for The Behavioral Sciences (MindTap C...StatisticsISBN:9781305504912Author:Frederick J Gravetter, Larry B. WallnauPublisher:Cengage Learning
- Elementary Statistics: Picturing the World (7th E...StatisticsISBN:9780134683416Author:Ron Larson, Betsy FarberPublisher:PEARSONThe Basic Practice of StatisticsStatisticsISBN:9781319042578Author:David S. Moore, William I. Notz, Michael A. FlignerPublisher:W. H. FreemanIntroduction to the Practice of StatisticsStatisticsISBN:9781319013387Author:David S. Moore, George P. McCabe, Bruce A. CraigPublisher:W. H. Freeman