MATLAB: Txt file and matlab??? help

matlab functiontext file

okay so i have to use a function that i created to change a list of numbers in the text file. the list of numbers are pound and inchs and i have to convert them to cm and kg, however every time i use the function program it changes the whole list into centimetre instead of changing it into centimetre and kilogram. any help would be great. so my function is :
function[centimeter,kilogram]=IPtoCK(inch,pound)
for k=1:44;
if k<=77.4
centimeter=(inch.*2.54);
else
kilogram=(pound.*0.4536);
end
end
end

Best Answer

  • That’s a fairly complicated file to read, but this works:
    fidi = fopen('heightweight.txt');
    D = textscan(fidi, '%f%f', 'HeaderLines',8, 'TreatAsEmpty',{'\' '}'}, 'EndOfLine','\r\n');
    fclose(fidi);
    inch = D{1};
    inch = inch(~isnan(inch));
    pound = D{2};
    pound = pound(~isnan(pound));
    Then expressing your function (saving it to IPtoCK.m) as:
    function [centimeter,kilogram]=IPtoCK(inch,pound)
    centimeter=(inch.*2.54);
    kilogram=(pound.*0.4536);
    end
    and calling it as:
    [centimeter,kilogram]=IPtoCK(inch,pound)
    is all you need.