Quantcast
Channel: MATLAB Central Newsreader Recent Posts
Viewing all articles
Browse latest Browse all 19628

Re: textscan question

$
0
0
On 12/20/2012 3:22 PM, t wrote:
...
> I have a text file (sample.txt):
> A 1 1
> B 3 4
> C X 5
> D 1 2
>
> Columns 2 and 3 are supposed to be all numbers. But sometimes, we get
> dirty rows with some characters (e.g. X in line 3)
>
> I am reading this file using these lines:
>
> fid = fopen('sample.txt','r');
> hh = textscan(fid, '%s%f%f', 'ReturnOnError', 0);
> fclose(fid);
>
> When I run it, matlab complains
> Error using textscan
> Mismatch between file and format string.
...

 >> fid=fopen('sample.dat','rt');
 >> c=textscan(fid,'%s %f %f','treatasempty',{'X'})
c =
     {4x1 cell} [4x1 double] [4x1 double]
 >> c{2}
ans =
      1
      3
    NaN
      1
 >> fid=fclose(fid);
 >>

Presuming you can define the set of bum characters...

--

Viewing all articles
Browse latest Browse all 19628

Trending Articles