Calculating average from data file?
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 27 Oct 2013
avg = mean(a);
Catégories
En savoir plus sur Other Formats dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!