Nathan Tornquist
Nathan Tornquist

Reputation: 6609

Matlab File Reading

I have a file that has numerous data points, all formatted like XX.XXX The numbers are strung together continuously.

How do I separate all the numbers into individual values?

I tried reading from the file with '%5.3d' and '%5.3f' but neither of them worked. Likewise, '%6s' did not work.

Example Input:

75.91425.43937.55492.55874.43839.51519.59935.11762.33178.14914.81569.43037.90083.32590.492
70.45060.24634.07148.20638.34348.88881.90070.37655.06531.76382.54791.43659.88274.56288.827
87.28590.39641.39551.67340.39870.613

Desired Output:

75.914
25.439
37.554
92.558
etc.

I have tried:

fscanf(Fid,'%6.3f',[3 inf]);
fscanf(Fid,'%5.3f',[3 inf]);
fscanf(Fid,'%5.3f',[3 inf]);
fscanf(Fid,['%2d' char('.') '%3d'], [6 inf]);

Upvotes: 0

Views: 294

Answers (1)

Aero Engy
Aero Engy

Reputation: 3608

Assuming every element is 6 characters you could do the following since fscanf isn't working. Note: I saved your sample into "numData.txt"

EDIT: This is much better.

fid = fopen('numData.txt','r');
numArray = textscan(fid,'%6.3f',inf);

gives

numArray =

   75.9140
   25.4390
   37.5540
   ... etc

Upvotes: 2

Related Questions