2

I have to read a file like this

10001   3          5.0000      30.0         0.0000      25.6         0.0000      10.0
10002   1         25.0000                   0.0000                   4.6887      58.2
10003   5         45.0000      20.0         0.0000                  14.7608          
10004   5         65.0000                   0.0000                   8.8791          
10005   1         85.0000                   0.0000                   6.3128      00.0

where the file format like this '%5i%5i%%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f'

I'm using the following code

n_xyz_filename = input('\nSelect the file. ', 's');
n_xyz_file = fopen(n_xyz_filename, 'r');
n_xyz = textscan(n_xyz_file, '%5i%5i%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f');
fclose(n_xyz_file);

But I keep on getting the following error

??? Error using ==> textscan Badly formed format string.

I really can't get it!

EDIT

As the answer said, the right code is:

n_xyz_filename = input('\nSelect the file. ', 's');
n_xyz_file = fopen(n_xyz_filename, 'r');
n_xyz = textscan(n_xyz_file, '%5d%5d%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f');
fclose(n_xyz_file);

with the "d" (stand for decimal) instead of "i"

1 Answers1

2

The problem is the format specifier i, which is not recognized by textscan. In case you wanted to indicate an integer, you should've used d. The correct syntax is therefore:

n_xyz = textscan(n_xyz_file, '%5d%5d%10.4f%8.1f%10.4f%8.1f%10.4f%8.1f');
Eitan T
  • 32,660
  • 14
  • 72
  • 109
  • 1
    Yeah! That almost done it! But now I have another problem: the matrix n_xyz now is displayed as following `n_xyz = [3498x1 int32] [3498x1 int32] [3498x1 double] [3498x1 double] [3498x1 double] [3498x1 double] [3498x1 double] [3497x1 double]` and each components are show as vector – Michele Redaelli Apr 29 '13 at 12:55
  • @MicheleRedaelli There is no problem: It seems that your file contains 3498 lines (and obviously 8 columns, like specified in the format string). Each cell therefore contains all the extracting values from the corresponding column. To access each cell, use curly braces (`{}`), for instance the values of the second column are `n_xyz{2}`... – Eitan T Apr 29 '13 at 14:13
  • 1
    thanks a lot You made my day! I hate professor asking you to do something w/o explaining the basic! – Michele Redaelli Apr 29 '13 at 19:46