Question
Help writing MatLab code

Lab Assignment 5-Employees Average Hours CSCI 251 Problem Statement Suppose the weekly hours for all employees are stored in a text file where each line contains the employee name followed by the hours worked for each day of the week. The data is stored as follows (data is continuous in the file but represented in columns below): Kelly Brian Katie Michae Emily Jim John Jane Joe Doe Smith Hart Jones Hu Wright Young Green Hurley Write a program that reads the contents from the file weeklyHours.txt, and outputs the name of the employee and the average number of hours worked that week. Input Data Contents read from weeklyHours.txt Name followed by 7 numbers (hours worked for the 7 days of the week) output Data Name Average hours for the week Getting Started 1. On Blackboard, download and save weeklyHours.txt 2. In Command Window type edit hourly.m. You will be prompted that the file does not currently exist; select okay. MATLAB will open the file in the editor. 3. Include header comments formatted as show below (you may want to copy and paste the header from a previous lab assignment and edit it).
media%2F05d%2F05d3c804-b786-4776-81d0-05
0 0
Add a comment Improve this question Transcribed image text
Answer #1

MATLAB Code:

% MATLAB Script that reads employee working hours data and calculates average working hours

clc;
clear;

% Opening file in read mode
filehandle = fopen("weeklyHours.txt", "r");

% Array to hold data
employeeHours = {};

% For iterating over each employee
i = 1;
j = 1;

% Reading names from file
if(~feof(filehandle))

    % Fetching a line of data
    line = fgetl(filehandle);
  
    % Splitting on space
    cols = strsplit(line);
  
    for i = 1:2:18
  
      % Forming name
      name = [cols{i} ' ' cols{i+1}];
  
      % Storing name
      employeeHours{j,1} = name;
      employeeHours{j,2} = [];
      employeeHours{j,3} = 0;
    
      j = j+1;
    
    endfor

endif

% Reading names from file
while(~feof(filehandle))

    j = 1;
  
    % Fetching a line of data
    line = fgetl(filehandle);
  
    % Splitting on space
    cols = strsplit(line);
  
    % Iterating over each employee
    for i = 1:length(employeeHours)
  
      % Storing hours worked
      employeeHours{j,2} = [employeeHours{j,2} str2num(cols{i})];
    
      j = j+1;
    
    endfor
  
endwhile

j = 1;

% Calculating average working hours
for i = 1:length(employeeHours)
  
% Forming name
employeeHours{j,3} = sum(employeeHours{j,2}) / 7.0;
    
j = j+1;
    
endfor


% Closing file
fclose(filehandle);

% Displaying data
for j = 1:i

    % Printing formatted String of data
    fprintf("\n %s average hours worked: %.2f ", employeeHours{j, 1}, employeeHours{j, 3});

endfor

fprintf("\n\n");

_______________________________________________________________________________________________

Sample Output:

Command Window John Doe average hours worked: 4.86 Jane Smith average hours worked: 4.00 Joe Hart average hours worked: 2.86

Add a comment
Know the answer?
Add Answer to:
Help writing MatLab code Lab Assignment 5-Employees Average Hours CSCI 251 Problem Statement Suppose the weekly...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT