Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

For a biomedical experiment, the names and weights of some patients have been st

ID: 3824841 • Letter: F

Question

For a biomedical experiment, the names and weights of some patients have been stored in a file patwts.dat. For example, the file might look like this:

Darby George 166.2

Helen Dee 143.5

Giovanni Lupa 192.4

Cat Donovan 215.1

Create this data file first. Then, write a script readpatwts that will first attempt to open the file. If the file open is not successful, an error message should be printed. If it is successful, the script will read the data into strings, one line at a time. Print for each person the name in the form ‘last,first’ followed by the weight. Also, calculate and print the average weight. Finally, print whether or not the file close was successful. For example, the result of running the script would look like this:

>> readpatwts

George,Darby 166.2

Dee,Helen 143.5

Lupa,Giovanni 192.4

Donovan,Cat 215.1

The ave weight is 179.30

File close successful.

MATLAB also need to know how to create the file to begin with. Thanks!

Explanation / Answer

-----Program to create cpatwts.dat file.

%Creating patwts.dat file
C ={'George','Darby',166.2;'Dee','Helen',143.5,;'Lupa','Giovanni',192.4;'Donovan','Cat',215.1}
%Open a file named cpatwts.dat for writing
fileID = fopen('patwts.dat','w');
formatSpec = '%s %s %2.1f ';
%Determine the size of C and write to file
[nrows,ncols] = size(C);
for row = 1:nrows
fprintf(fileID,formatSpec,C{row,:});
end
fclose(fileID);

Solution to part B:

fileID = fopen('patwts.dat','w');
%check if file is opened
if fileID ~= -1
formatSpec = '%s,%s %2.1f ';
[nrows,ncols] = size(C);
for row = 1:nrows
fprintf(fileID,formatSpec,C{row,:});
end
n=input(166.2,143.5,192.4,215.1,179.30)
for x=[1:n]
x = x+1
   avgmsg = strcat('The ave weight is'disp(x));
end
fclose(fileID);
successmsg = strcat('File close successful.');
else
errormsg = strcat('The file is not opened!');

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote