Calculating average from data file?
Show older comments
I have the following function to open a data file patwts.
I am trying to find out how to take the average of numbers respective to each person in the list.
Darby George 166.2
Helen Dee 143.5
Giovanni Lupa 192.4
Cat Donovan 215.1
(there is a space between each line in the data)
I have my function to be this so far:
function [ avgw ] = readpatwts( path )
FID = fopen(path, 'r');
patha = textscan(FID, '%f');
a = patha{2};
if FID ~= -1
fprintf('FILE OPENED SUCCESSFULLY!\n');
avg =sum(a)./4;
avgw = fprintf('THE AVG WEIGHT IS: %s.\n',avg);
else
fprintf('ERROR CANNOT OPEN FILE TO READ!\n');
end
if fclose(FID) == 0;
fprintf('FILE CLOSED SUCCESSFULLY!\n');
else
fprintf('FILE NOT CLOSED SUCCESSFULLY!\n');
end
end
The portion that is the problem is finding the avg from the numbers in the data file.
How could I do this?
Accepted Answer
More Answers (1)
Walter Roberson
on 27 Oct 2013
avg = mean(a);
Categories
Find more on Other Formats in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!