Calculating average from data file?

16 views (last 30 days)
Nora
Nora on 27 Oct 2013
Edited: Mark Mikofski on 6 Nov 2013
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

Mark Mikofski
Mark Mikofski on 27 Oct 2013
You need to read in the first and last name, assuming that there are always just two names.
patha = textscan(fid,'%s %s %f');
This yields:
patha =
{4x1 cell} {4x1 cell} [4x1 double]
So then you need to grab the 3rd column
a = patha{3};
Then just use mean
avg = mean(a)
Another option to textscan is regexp
function avgw = readpatwts(path)
FID = fopen(path, 'r');
if FID ~= -1 fprintf('FILE OPENED SUCCESSFULLY!\n');
patha = read(FID); % read the entire contents
a = regexp(path,'\d+.\d+','match'); % return cell array of numbers
avg = mean(a);
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
Also you could use a try and catch block and assert instead of if statements. And why not return the average you calculated instead of the number of characters printed?
function avg = readpatwts(path)
FID = fopen(path, 'r');
assert(FID~=-1,'readpatwts:IOerror','ERROR CANNOT OPEN FILE TO READ!')
fprintf('FILE OPENED SUCCESSFULLY!\n')
try
patha = read(FID); % read the entire contents
catch ME
fclose(fid)
rethrow(ME)
end
fclose(fid)
fprintf('FILE CLOSED SUCCESSFULLY!\n')
a = regexp(path,'\d+.\d+','match'); % return cell array of numbers
avg = mean(a);
fprintf('THE AVG WEIGHT IS: %s.\n',avg)
end
  3 Comments
Walter Roberson
Walter Roberson on 27 Oct 2013
ME is created by try/catch when an error is caught. Or, more correctly, the error structure already exists, created by some error() call, and "catch ME" indicates that the name one should locally associate with that structure is "ME".
Mark Mikofski
Mark Mikofski on 28 Oct 2013
Edited: Mark Mikofski on 6 Nov 2013
regexp uses regular expressions, which is present in almost every computing language (EG: MATLAB, Python, Ruby, Java, etc.)
matches = regexp(string, pattern, 'match')
looks for the "pattern" in "string" and returns it in "matches". The "pattern" in this case is one or more digits, "\d+", then a decimal point, ".", followed by one or more additional digits. Regex has several shortcuts for character groups, "\d" is a short cut for "match any of the numerical characters, 0,1,2,3,4,5,6,7,8 and 9". The plus sign, "+", indicates at least one or more matches. It takes a while to get the hang of regex, but there are lots of regex testers online. Learning regex is a valuable skill and will save you lots of time, so the return on investment is worth it.
A try and catch block is a very important programming idiom, that is also present in every language, although the syntax varies (EG: Python and Java both use try and except). If any errors are thrown (or raised in Python terminology) in the code immediately after the try statement, they are captured, in MATLAB in a MException structure as Walter indicated, and execution of the code moves to the catch statement, where you can decide what to do based on the type of error. You should study the try/catch section in the MATLAB user guide.
An assertion is just a way of throwing an error in MATLAB.
assert(FID~=-1,'readpatwts:IOerror','ERROR CANNOT OPEN FILE TO READ!')
means only continue execution if FID is not negative-one, otherwise throw an error with my custom error ID "readpatwts:IOerror" that I just made up on the spot, and also give the user this error message using STDERR (FID=2, red colored font in the MATLAB Command Window) "ERROR CANNOT OPEN FILE TO READ!" The assert command is very similar to the error command. Both can be useful, especially combined with a try and catch block to handle expected bad outcomes gracefully.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 27 Oct 2013
avg = mean(a);

Categories

Find more on Data Type Identification 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!