Effacer les filtres
Effacer les filtres

Issues with script writing mixed text/numeric data to file as a table

5 vues (au cours des 30 derniers jours)
Hello, I am new to MATLAB and need to use a user-defined function IPtoCK to convert a set of people's height and weight in inches and pounds to centimeters and kilograms, respectively, but then write the resulting data as well as calculated BMIs and BMI categories (i.e. underweight, normal, overweight, obese) into a formatted table in a text file. My user defined function works well enough but the output written to BMI.txt is not formatted correctly with one decimal place for all numeric values as I need them to be, no matter how I specify. They also only export some of the height data in centimeters to the text document, and in a matrix that appears to be of inconsistent delimiters and dimensions(?). I decided to enter the category entries as character arrays rather than strings so that they are all 1x1 vector containers rather than string arrays of various sizes, but I am also unsure of how to write the character arrays into a column vector in order to concatenate it onto the table with the numeric values, or otherwise have it show up as the last column in BMI.txt. Any advice would be appreciated! Thank you!!
Here is what the code writes to BMI.txt:
Height(cm) Weight(kg) BMI Category
148.0 171.0 177.0162.0 169.0 173.0164.0 170.0 174.0161.0 178.0 181.0173.0 170.0 158.0175.0 158.0 186.0175.0 173.0 166.0148.0 158.0 174.0194.0 177.0 173.0165.0 171.0 180.0167.0 189.0 158.0166.0 171.0 150.0176.0 171.0 171.0178.0 180.0 178.0165.0 170.0 174.0157.0 160.0 166.0171.0 188.0 45.2 72.0 66.8 75.9 70.4 67.6 66.9 66.4 79.7 58.2 62.2 77.8 82.9 56.1 55.6 79.7 43.0 86.1 77.4 70.8 57.0 50.3 58.1 78.1 94.1 68.8 83.4 62.2 60.1 70.3 71.3 84.3 64.1 64.0 89.1 40.5 67.4 66.5 68.0 86.7 76.7 71.8 63.2 73.0 63.4 60.0 46.9 62.7 59.0 74.4 20.6 24.6 21.3 28.9 24.6 22.6 24.9 23.0 26.3 22.5 19.6 23.7 27.7 19.4 22.3 26.0 17.2 24.9 25.3 23.7 20.7 23.0 23.3 25.8 25.0 22.0 27.9 22.8 20.6 21.7 25.6 23.6 25.7 23.2 30.5 18.0 21.8 22.7 23.3 27.4 23.7 22.7 23.2 25.3 20.9 24.3 18.3 22.8 20.2 21.0
And here is my code:
IP=readmatrix('HeightWeightIP.txt');
format shortG
INCHES=[IP(:,1)];
POUNDS=[IP(:,2)];
I=INCHES;
P=POUNDS;
[C,K]=IPtoCK(INCHES,POUNDS);
CENTIMETERS=C;
KILOGRAMS=K;
BMI=[KILOGRAMS./(0.01.*CENTIMETERS).^2];
for CK=[1:length(BMI)]
if BMI(CK)<18.5
Category="underweight";
elseif BMI(CK)>=18.5 & BMI(CK)<25
Category="normal";
elseif BMI(CK)>=25 & BMI(CK)<30
Category="overweight";
else
Category="obese";
end
BMICAT=[BMI(CK)]
end
fid=fopen('BMI.txt','w');
fprintf(fid, 'Height(cm) Weight(kg) BMI Category\n')
fprintf(fid, '%5.1f %4.1f %4.1f', CENTIMETERS, KILOGRAMS, BMI)
  4 commentaires
Star Strider
Star Strider le 12 Mai 2024
Unrecognized function or variable 'IPtoCK'.
.
Walter Roberson
Walter Roberson le 12 Mai 2024
Note that every iteration, you are overwriting all of Category and all of BMICAT .
Note that you do not use Category or BMICAT after the loop. You might as well not have the loop at all.

Connectez-vous pour commenter.

Réponse acceptée

Walter Roberson
Walter Roberson le 12 Mai 2024
fprintf(fid, '%5.1f %4.1f %4.1f', [CENTIMETERS, KILOGRAMS, BMI] .')
fprintf() works by first using up all of the second parameter, then all of the third parameter, then all of the 4th parameter... So you have to arrange the elements down columns to get it to work right.
See also compose which knows about using corresponding parameters.
  3 commentaires
Walter Roberson
Walter Roberson le 12 Mai 2024
fprintf(fid, '%s\n', compose("%5.1f %4.1f %4.1f", CENTIMETERS, KILOGRAMS, BMI));
Sydney
Sydney le 12 Mai 2024
Thank you! This works

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Tables dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by