简体   繁体   English

如何从Matlab中的文本文件中仅读取数字列?

[英]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. 我有一个文本文件,其中有11列(由|分隔)和许多行。 Some columns only have numeric values(eg age), some have only strings(eg name) and some columns have both(eg 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. 如上所述,我只想获取第4、5、6、7、8和10列。

I have tried dlmread, textscan, csvread, tdfread. 我尝试了dlmread,textscan,csvread,tdfread。 But in each time I got errors. 但是每次我都会出错。

A simple method using textscan. 使用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. 我不知道第9列中的值是什么,因此我使用了限定符字符串。 In this case, the white space are displayed correctly if you use %d we get zero. 在这种情况下,如果您使用%d,则空白显示正确,我们得到零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM