Calculating BMI based on data imported to MatLab

24 views (last 30 days)
Hi there. I am stuck on this homework assignment. I'm very new to Matlab and am not really sure where to start.
Write a Matlab program that reads the height and weight data. Note that this is a text file that has one row with header information. Use the command "textscan" to read in the data. Use the "HeaderLines" argument to remove the header row.
Write a function that calculates the Body-Mass index (BMI) for each player. This is calculated as follows:
BMI = 703 * ( weight in pounds ) / ( height in inches x height in inches )
Calculate the BMI for each player in the data set. What is the name of the player with the highest BMI?
fid=fopen('heightweight2.txt');
I know at some point I have to use textscan. But I am not sure how to write the formatspecs. Could someone point me in the right direction?
Thank you.

Answers (2)

Image Analyst
Image Analyst on 11 Feb 2015
Try readtable() instead
t = readtable(filename);
  2 Comments
Stacie Sanchez
Stacie Sanchez on 11 Feb 2015
Thank you for your input. I'll give that a go and see what that does.
Image Analyst
Image Analyst on 12 Feb 2015
Why mess with textscan when readtable is so easy?
fileName = 'heightweight2.txt';
t = readtable(fileName, 'delimiter', '\t')
It gets all the columns into columns in table, t.

Sign in to comment.


Star Strider
Star Strider on 11 Feb 2015
The variable 3 is probably ‘fid’. This is the file identifier for input and output reading and writing, depending on what you want to do. The standard output is 1 (using it prints text in black in the Command Window), the error output is 2 (using it prints text in red in the Command Window). So other file identifiers are numbered starting at 3. If you opened another file to print your results to, it would have file identifier = 4.
You would use the textscan function to read those files. (You will need to use at least the 'HeaderLines' and 'Delimiter' name-value pairs as input arguments to textscan.) That otherwise seems a relatively straightforward file to read, so I won’t deprive you of that pleasure by writing the code for you.
  2 Comments
Stacie Sanchez
Stacie Sanchez on 11 Feb 2015
Yeah, I figured out the three bit later. Thank you for your input. I'm still trying to figure out what the hell each of the formatspecs do and what delimiters are.
Star Strider
Star Strider on 12 Feb 2015
I was just about to reply when ‘Answers’ went down for three hours. Sorry for the delay.
The formatSpec tell textscan how you want to read in the data in the file. You might experiment with '%s%s%s%f%f%f' and see how that works. The first three fields in the ‘heightweight2’ file are all strings, the last three all numeric.
Delimiters separate the fields (or columns) in each line of the file. These are most often a space, denoted by ' ', and the tab, denoted by '\t'. Consider the other ‘name-value pair’ input arguments as necessary, although the file is likely not so complicated that you’ll need to use any of them. (I haven’t attempted to download and read it, preferring to let you have that fun. So everything here is untested code.)
If you have problems, we’ll help. If your code throws errors, copy the entire red text, including the line that threw the error, and paste it into your comment. Otherwise, describe what you want your code to do, and what it actually does.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!