MATLAB: String Manipulation and File Input/Output -
ok have data file similar 1 containing names , weights of people:
darby george 166.2 helen dee 143.5 giovanni lupa 192.4 cat donovan 215.1
im supposed read script strings 1 line @ time, save weight vector, , print each person's name in form 'last,first' followed weight:
george, darby’s weight 166.20 lbs. dee, helen’s weight 143.50 lbs. lupa, giovanni’s weight 192.40 lbs. donovan, cat’s weight 215.10 lbs.
this code:
fid = fopen('patwts.dat'); if fid == -1 disp('file open not successful') else while feof(fid) == 0 % read 1 line string variable aline = fgetl(fid); %save vector , fprintf here strtok(aline,' ')=[first last num]; fprintf('%s %s %3.2f',last,first,num) mat=['%3.2f %3.2f %3.2f %3.2f %3.2f %3.2f 3.2f %3.2f %3.2f %3.2f'] end closeresult = fclose(fid); if closeresult == 0 disp('file close successful') else disp('file close not successful') end end fprinf('the average weight $sum(%3.2f)/2 lbs', num)
i'm having problems strtok
function.
i don't know how format string read 'last, first' weight
.
[token, remain] = strtok(aline,' ')
return first token "darby" , remain of line. don't think looking for. suggest use strsplit(aline, ' ')
, return cell containing tokens , need each "first, last, num" doing loop (for example)
Comments
Post a Comment