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

MATLAB For a biomedical experiment, the names and weights of the patients have b

ID: 3737403 • Letter: M

Question

MATLAB

For a biomedical experiment, the names and weights of the patients have been stored in a file ‘PatWts.dat’ (bundled in the .zip file). For example, the file may look like this:

Darby George 166.2

Helen Dee 143.5

Giovanni Lupa 192.4

Cat Donovan 215.1

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 use fgetl to read the data into strings, one line at a time. Do not assume that the number of lines in the file is known. Instead of using a for loop, use a while loop until the end of the file is reached. Then your script will perform the following additional steps: (1) print for each person the name in the form ‘last,first’ followed by the weight; (2) calculate and print the average weight. Finally, your script will use fclose to close the file and print whether or not the file was successfully closed.

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

Explanation / Answer

Solution:

The script is given below:

fid = fopen('readpatwts.m');

tline = fgetl(fid); count = 1;

while ischar(tline) % disp(tline) % this line is name.

tline = fgetl(fid);

patData(count).name= tline; % this assign field of the structure

% this line is weight

tline = fgetl(fid);

% Next line is the weight.

patData(count).weight=str2double(tline);

% Moves to the next structure count = count + 1;

end

fclose(fid);

I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)