txt file and matlab??? help

1 view (last 30 days)
dena hosseini
dena hosseini on 5 Apr 2015
Edited: Star Strider on 6 Apr 2015
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

Accepted Answer

Star Strider
Star Strider on 6 Apr 2015
Edited: Star Strider on 6 Apr 2015
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.
  1 Comment
Image Analyst
Image Analyst on 6 Apr 2015
So, with help from the others, here's the complete program (hope this was not your homework).
function test2 % Call the m-file whatever you want.
try
fidi = fopen('heightweight.txt');
D = textscan(fidi, '%f%f', 'HeaderLines',8, 'TreatAsEmpty',{'\' '}'}, 'EndOfLine','\r\n');
fclose(fidi);
height = D{1}(1:44);
height = height(~isnan(height))
weight = D{2};
weight = weight(~isnan(weight))
[cm, kg] = IPtoCK(height, weight);
fid=fopen('convert.txt', 'wt');
fprintf(fid,'Height [cm] Weight [kg]\n', cm, kg);
fprintf(fid,'%11.3f %11.3f\n', cm, kg);
fclose(fid);
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
WarnUser(errorMessage);
end
function[centimeter,kilogram]=IPtoCK(inch,pound)
try
centimeter = inch .* 2.54;
kilogram = pound .* 0.4536;
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
WarnUser(errorMessage);
end
function WarnUser(errorMessage)
uiwait(warndlg(warningMessage));

Sign in to comment.

More Answers (1)

Jan
Jan on 5 Apr 2015
Edited: Jan on 5 Apr 2015
Look at this piece of code:
for k=1:44
if k <= 77.4
...
k goes from 1 to 44. Then k is smaller equal 77.4 in every case.
In addition it is not clear, why you use a loop at all, because the contents of the loop does not depend on the loop counter. I guess this would help:
function [centimeter,kilogram]=IPtoCK(inch,pound)
centimeter = inch .* 2.54;
kilogram = pound .* 0.4536;
  2 Comments
dena hosseini
dena hosseini on 5 Apr 2015
Edited: Image Analyst on 5 Apr 2015
when i do it like this:
function[centimeter,kilogram]=IPtoCK(inch,pound)
centimeter = inch .* 2.54;
kilogram = pound .* 0.4536;
end
it gives an error, saying not enough input argument. this is what i write in my script file: The heightweight.txt is the text file that contains the list of numbers
v=load('heightweight.txt');
z=IPtoCK(v);
fid=fopen('convert.txt', 'w');
fprintf(fid,'%.3f \n',z);
fclose(fid);
Image Analyst
Image Analyst on 5 Apr 2015
It requires two arguments, not one. You passed in only one, called v. You need to extract inch and pound. Maybe like this, depending on what v is:
inches = v.inch;
pounds = v.pound;
[cm, kg] = IPtoCK(inches, pounds);
fid=fopen('convert.txt', 'wt');
fprintf(fid,'%.3f %.3f\n', cm, kg);
fclose(fid);

Sign in to comment.

Categories

Find more on MATLAB 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!