找回密码
 注册
Simdroid-非首页
查看: 98|回复: 3

读取文本文件

[复制链接]
发表于 2013-6-17 19:07:23 | 显示全部楼层 |阅读模式 来自 山东青岛
如何读取ASCII文件,直接忽略里面的每行带#的注释
发表于 2013-6-17 19:27:54 | 显示全部楼层 来自 北京
Simdroid开发平台
可以参考一下论坛的正则系列的帖子
  1. s={'1adfadsfadsf'
  2.     '#2adsfadsfadsfadsfadsfadsf'
  3.     '3asdfasdfadsfadsfads'
  4.      '4adsfadsfadsfadsfadsfadsf'
  5.     '5asdfasdfadsfadsfads'
  6.      '#6adsfadsfadsfadsfadsfadsf'
  7.     '7asdfasdfadsfadsfads'
  8.      '#8adsfadsfadsfadsfadsfadsf'
  9.     '9asdfasdfadsfadsfads'
  10.     '#10adsfadsfadsfadsfadsfadsf'
  11.     '#11asdfasdfadsfadsfads'
  12.     }
  13. x=regexprep(s,'#\w*','')
复制代码
  1. >> x=regexprep(s,'#\w*','')

  2. x =

  3.     '1adfadsfadsf'
  4.     ''
  5.     '3asdfasdfadsfadsfads'
  6.     '4adsfadsfadsfadsfadsfadsf'
  7.     '5asdfasdfadsfadsfads'
  8.     ''
  9.     '7asdfasdfadsfadsfads'
  10.     ''
  11.     '9asdfasdfadsfadsfads'
  12.     ''
  13.     ''
复制代码
不过你首先需要把文本读进来:
  1. fid = fopen(name);
  2. data = textscan(fid, '%s',  'Delimiter', '\n');
  3. s=data{:}
复制代码

点评

此外空值需要删除 x=x(~cellfun(@isempty,x))  发表于 2013-6-17 21:47

评分

1

查看全部评分

回复 不支持

使用道具 举报

 楼主| 发表于 2013-6-17 19:59:56 | 显示全部楼层 来自 山东青岛
liuyalong008 发表于 2013-6-17 19:27
可以参考一下论坛的正则系列的帖子不过你首先需要把文本读进来:

问题补充,我的数据每行格式:
    1,00000,142,1,32400,N,159600,E, ship.obsDay04.rcvJan05
    2,00000,142,1,32100,N,158500,E, ship.obsDay04.rcvJan05
    3,00000,142,1,31400,N,135400,E, ship.obsDay04.rcvJan05
读取之后,需要根据每个字段的值删选数据,我是定义结构体容易操作,还是定义N个数组分别存储?
回复 不支持

使用道具 举报

发表于 2013-6-17 22:00:23 | 显示全部楼层 来自 北京
youknownwhat 发表于 2013-6-17 19:59
问题补充,我的数据每行格式:
    1,00000,142,1,32400,N,159600,E, ship.obsDay04.rcvJan05
    2,0000 ...

still  regular expression !!!
假设你的数据存放在cell中
  1. s={'1,00000,142,1,32400,N,159600,E, ship.obsDay04.rcvJan05'
  2.    '2,00000,142,1,32100,N,158500,E, ship.obsDay04.rcvJan05'
  3.    '3,00000,142,1,31400,N,135400,E, ship.obsDay04.rcvJan05'}
复制代码
step: extract digits
  1. d=regexp(s,'\d*','match')
  2. t=reshape([d{:}],[],numel(d))'
复制代码
  1. t =

  2.     '1'    '00000'    '142'    '1'    '32400'    '159600'    '04'    '05'
  3.     '2'    '00000'    '142'    '1'    '32100'    '158500'    '04'    '05'
  4.     '3'    '00000'    '142'    '1'    '31400'    '135400'    '04'    '05'
复制代码
step2: get Lat or Lon (i guess)
  1. L=regexp(s,'[SNEW]','match')
  2. Sign=reshape([L{:}],[],numel(L))'
复制代码
  1. Sign =

  2.     'N'    'E'
  3.     'N'    'E'
  4.     'N'    'E'
复制代码

评分

1

查看全部评分

回复 不支持

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|小黑屋|联系我们|仿真互动网 ( 京ICP备15048925号-7 )

GMT+8, 2024-6-21 06:39 , Processed in 0.043318 second(s), 16 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表