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
- Algebra: Structure And Method, Book 1AlgebraISBN:9780395977224Author:Richard G. Brown, Mary P. Dolciani, Robert H. Sorgenfrey, William L. ColePublisher:McDougal LittellHolt Mcdougal Larson Pre-algebra: Student Edition...AlgebraISBN:9780547587776Author:HOLT MCDOUGALPublisher:HOLT MCDOUGALMathematics For Machine TechnologyAdvanced MathISBN:9781337798310Author:Peterson, John.Publisher:Cengage Learning,
- Trigonometry (MindTap Course List)TrigonometryISBN:9781305652224Author:Charles P. McKeague, Mark D. TurnerPublisher:Cengage Learning