Sunday, 15 May 2011

MATLAB: String Manipulation and File Input/Output -


OK, I have such a data file that contains people's names and weights:

  Darby George 166.2 Helen D143.5 Giovanni Lupa 19.4 Cat Donovan 215.1  

Im reading strings in one line at one time, saving weight in vector and for every print person The name is in weight as 'last, first':

  George, Darby's weight is 166.20 pounds. D, Helen weighs 143.50 lbs. Lupa, Giovanni's weight is 192.40 lbs. Donovan, the weight of the cat is 215.10 lbs.  

This is my code:

  fid = fopen ('patwts.dat'); If fid == -1 disp ('file is not open') and while feof (fid) == 0% read one line in a string variable aline = fgetl (fid); % Save vector and fprintf here strtok (aline, '') = [first final number]; Fprintf ('% s% s% 3.2f', last, first, number) mat = ['% 3.2f% 3.2f% 3.2f% 3.2f% 3.2f% 3.2f 3.2f% 3.2f% 3.2f% 3.2' F '] and closurebel = ficus (fidd); If the adjacent == 0 Disp ('file closed successful') and Disp ('file not successful') end-end fprinf ('average weight $ yoga (% 3.2f) / 2 lbs' is the number)  < / Pre> 

I'm having a problem with the strtok function

I do not know how to read the string

'last, first' To be formatted for.

will remain the first token" Darby "and line. This is what you want. I would suggest to use the strsplit (aline, '') , it will return all the token cells and you need to get every "first, last, number" for loop Example)


No comments:

Post a Comment