The Body Mass Index, or BMI

Hi I tried to write a function called findbmi that would receive the weight and height as input arguments, and would return the BMI but for some reason it's not working. Please help
this is my code
Weight= input('Enter your weight in lbs, or typer 0 for SI units:')
if Weight==0;
Weight=input('Enter your weight in kg:')
Height=input('Enter your height in m:')
function BMI=findbmi(Weight,Height)
BMI= (Weight/(Height^2)) %kg/m^2
else
Height=input('Enter your height in ft:')
function BMI=findbmi(Weight,Height)
BMI=(Weight/(Height^2))*4.882427111
end

Answers (1)

Just get rid of these lines:
function BMI=findbmi(Weight,Height)

8 Comments

yeah but I have to use findbmi in my matlab T.T
it's the requirement
Dai Nguyen
Dai Nguyen on 9 Sep 2020
Edited: Dai Nguyen on 9 Sep 2020
This is my new code
function bmi=findbmi(weight,height)
bmi= (weight/(height^2));
if nargin==0
weight=input('Enter your weight in kg:');
height=input('Enter your height in m:');
end
It showed an error "Function definition not supported in this context. Create functions in code"
file.
Was this the only code in your file? You should try to avoid mixing script files and functions.
@Dai: Create two separate files.
% findbmi_script.m
Weight= input('Enter your weight in lbs, or typer 0 for SI units:')
if Weight==0
Weight=input('Enter your weight in kg:');
Height=input('Enter your height in m:');
BMI=findbmi(Weight,Height);
else
Height=input('Enter your height in ft:');
BMI=findbmi(Weight,Height)*4.882427111;
end
And a separate file with the BMI calculation:
% findbmi.m
function bmi=findbmi(weight,height)
bmi= (weight/(height^2));
Then run your script from the command line:
findbmi_script
it said "Unrecognized function or variable 'findbmi_script'." for some reason
oh nvm I got it
thank you

Sign in to comment.

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Release

R2020a

Asked:

on 9 Sep 2020

Commented:

on 9 Sep 2020

Community Treasure Hunt

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

Start Hunting!