How to read only the numeric columns from a text file in matlab?

I have a text file where there are 11 columns(separated by |) and many rows. Some columns only have numeric values(e.g. age), some have only strings(e.g. name) and some columns have both(e.g. address). What I want to do is to get only the numeric columns (columns which there are only numbers). In some places in those numeric columns you can also find blanks.

A few rows are given below...

1415 E CENTRAL RD  |ARLINGTON HEIGHTS|IL|60005|1|45.50 |.00|1|b| |C
10733 GRAND AV     |MELROSE PARK     |IL|60164| |45.50 |.00|0|a|1|A
1029 CHARLELA LN 20|ELK GROVE        |IL|60007| |45.50 |.00|1|a| |C

As given above, I want to get only the columns 4, 5, 6, 7, 8 and 10.

I have tried dlmread, textscan, csvread, tdfread. But in each time I got errors.

Upvotes: 0

Views: 868

Answers (1)

diletant
diletant

Reputation: 13

A simple method using textscan.

file=('1.txt');
format=('%*s%*s%*s%d%d%f%f%d%*c%s%*c');
fid=fopen(file);
data=textscan(fid,format,'delimiter','|');

I do not know what should be the value in column 9, so I used the qualifier string. In this case, the white space are displayed correctly if you use %d we get zero.

Upvotes: 1

Related Questions