I'm struggling to get fprintf to work
Show older comments
Hi,
I've decided to learn how to code in MATLAB in my spare time. I've been working through some exercises and I've been working on the following problem.
Problem
Write a user-defined MATLAB function, with two input and two output arguments that determines the height in centimetres (cm) and mass in kilograms (kg)of a person from his height in inches (in.) and weight in pounds (lb).
(a) Determine in SI units the height and mass of a 5 ft.15 in. person who weight 180 lb.
(b) Determine your own height and weight in SI units.
I've managed to get the script to calculate my outputs correctly however, I can't get fprintf to work. I am able to output the variables with the disp command but I want to have both text and numbers displayed. Also would someone be able to have a look over this and let me know if this was the best approach to take when solving this problem.
Thank you!
function [ cheight , kweight ] = bodyconversion ( fheight , iheight , lweight )
%convert the weight of a person from lb's to kg's and the height from ft
%and inches to cm's
%fheight = height in feet, iheight = feet in inches and lweight = weight in
%lbs
fheight = input ( 'Enter your height in feet here: ' ) ;
iheight = input ( 'Enter your height in inches here: ' ) ;
lweight = input ( 'Enter your weight in pounds here: ' ) ;
%conversion formula for feet to inches is in = ft * 12
convertin = fheight * 12 ;
%conversion formula from inches to centimeters is: cm = inch * 2.54
% where height in inches includes feet.
cheight = ( convertin + iheight ) * 2.54 ;
% convert weight from lbs to kg formula is 1kg = 2.205 lbs
kweight = lweight / 2.205 ;
fprintf ( '%.5 ft, %5 in is %.5cm\n' , fheight , iheight , cheight ) ;
fprintf ( '%.5 lbs is %.5 kg\n' , lweight , kweight )
end
Accepted Answer
More Answers (0)
Categories
Find more on Calculus 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!