Calculating average from data file?
Mostrar comentarios más antiguos
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?
Respuesta aceptada
Más respuestas (1)
Walter Roberson
el 27 de Oct. de 2013
avg = mean(a);
Categorías
Más información sobre Other Formats en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!