查看: 623  |  回复: 6
【奖励】 本帖被评价2次,作者fspdlh增加金币 2 个
当前主题已经存档。

fspdlh

金虫 (正式写手)


[资源] 【原创】一个自编的读取文本文件数据的函数

如题,自编了一个函数,自已感觉用起来还是很方便的,如有需要可以下载。
    可以读取整个文件,读取第N行,读取第M行到第N行,读取第M行到倒数第N行,读取第M次(也可以是最后一次、倒数第M次)出现字符串str的行开始(或结束)的数据,等等。
以下是自编的帮助文档。
%Get text from file
%
%Input paraments:
%   fid:        filename or the indicater of the file
%   sea_tag:    find the target text from the beginningning of the text or
%               the current position
%       0:  the beginningning of the file   
%       1:  the current position
%   beg profix: paraments to point out the beginning of the output text
%   end profix: paraments to point out the endding of the output text
%   tag:        target string, a string inclued in the target line
%   mode:       the method to match the target line
%       1:  comletely matched
%       2:  beginning matched including blanks
%       3:  beginning matched ignoring blanks
%       4:  included
%   count:      the Nth of the target line
%   offset:     offset from the target line
%       negative ingeger: Nth line before from the target line
%       zero:   the target line
%       positive integer: Nth line followed from the target line
%
%The number of input paraments:
% 1     y=GetFileText(  fid )
% 2     y=GetFileText(  fid,    offset )
% 3     y=GetFileText(  fid,    sea_tag,    offset )
% 4     y=GetFileText(  fid,    sea_tag,    beg_offset, end_offset  )
% 6     y=GetLineText(  fid,    beg_sea_tag,   
%                       beg_str,beg_mode,   beg_count,  beg_offset  )
% 7     y=GetFileText(  fid,    beg_sea_tag,    beg_offset,
%                       end_str,    end_mode,   end_count,  end_offset  )
% 7     y=GetFileText(  fid,    beg_sea_tag,
%                       beg_str,    beg_mode,   beg_count,  beg_offset,
%                       end_offset  )
% 10    y=GetFileText(  fid,    beg_sea_tag,   
%                       beg_str,    beg_mode,   beg_count,  beg_offset,
%                       end_str,    end_mode,   end_count,  end_offset  )

[ Last edited by sunxiao on 2009-3-8 at 13:11 ]
回复此楼
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

fspdlh

金虫 (正式写手)


读取文件的例子

>> type a.txt

01
02
03  01
04
05
06  01
07
08 01
09
10

>> GetFileText('a.txt')

ans =

    '01'
    '02'
    '03  01'
    '04'
    '05'
    '06  01'
    '07'
    '08 01'
    '09'
    '10'

>> GetFileText('a.txt',4)

ans =

04

>> GetFileText('a.txt',0,4,-2)

ans =

    '04'
    '05'
    '06  01'
    '07'
    '08 01'
    '09'

>> GetFileText('a.txt',0,'01',4,1,0)

ans =

01

>> GetFileText('a.txt',0,'01',4,2,0)

ans =

03  01

>> GetFileText('a.txt',0,'01',4,2,-1)

ans =

02

>> GetFileText('a.txt',0,'01',4,2,1)

ans =

04

>> GetFileText('a.txt',0,'01',4,-1,-2)

ans =

06  01

>> GetFileText('a.txt',0,'01',4,-2,0)

ans =

06  01

>> GetFileText('a.txt',0,'01',4,-2,0,-1)

ans =

    '06  01'
    '07'
    '08 01'
    '09'
    '10'

>> GetFileText('a.txt',0,'01',4,1,0,'01',4,-1,0)

ans =

    '01'
    '02'
    '03  01'
    '04'
    '05'
    '06  01'
    '07'
    '08 01'

>> GetFileText('a.txt',0,3,'01',4,3,0)

ans =

    '03  01'
    '04'
    '05'
    '06  01'
    '07'
    '08 01'
2楼2009-03-02 22:43:40
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

fox-fox

木虫 (知名作家)


下载,试一下
3楼2009-03-03 08:55:18
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

woshilsh

荣誉版主 (职业作家)


★★★★★ 五星级,优秀推荐

鼓励!呵呵!加油!多发布优秀资源
4楼2009-03-03 09:15:06
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

xujunqun

银虫 (小有名气)


楼主辛苦
5楼2009-03-26 01:46:35
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖

physics206

新虫 (小有名气)


很不错!谢谢楼主分享
7楼2009-04-02 23:47:41
已阅   回复此楼   关注TA 给TA发消息 送TA红花 TA的回帖
简单回复
dnp6楼
2009-03-26 14:37   回复  
 
相关版块跳转 我要订阅楼主 fspdlh 的主题更新
☆ 无星级 ★ 一星级 ★★★ 三星级 ★★★★★ 五星级
普通表情 龙 兔 虎 猫 高级回复 (可上传附件)
最具人气热帖推荐 [查看全部] 作者 回/看 最后发表
[硕博家园] 极限拉扯,如何抉择 +6 Isak0320 2026-10-12 9/450 2026-10-12 21:13 by Isak0320
[基金申请] 入职后第一次管课题组的专利缴费,对账才发现官费比别人多掏了一倍 +6 13108017953 2026-10-08 6/300 2026-10-12 20:07 by w3w3y3
[有机交流] OpenAI又攻克数学难题了,离AI做有机合成还有多远? +4 wangyl_123 2026-10-10 8/400 2026-10-12 18:09 by wangyl_123
[教师之家] 课题组会帮你通过考核么? +3 好运来~9527 2026-10-10 6/300 2026-10-12 14:55 by 简单化xn
[公派出国] 售一区SCI文章T0P,我:8O.551.O54,科目全,可十急 +5 qR2AdCWTVtkw 2026-10-09 9/450 2026-10-11 22:34 by XQNyDCa1Tk5n
[公派出国] 售SCI一区文章,我:8O5.5.1.O5.4,科目全,可伽急 +5 qR2AdCWTVtkw 2026-10-09 10/500 2026-10-11 22:26 by XQNyDCa1Tk5n
[找工作] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +5 qR2AdCWTVtkw 2026-10-09 8/400 2026-10-11 22:26 by XQNyDCa1Tk5n
[考博] 售SCI一区T0P文章,我:8.O55.1.O.54,科目全,可十急 +5 qR2AdCWTVtkw 2026-10-09 9/450 2026-10-11 22:15 by XQNyDCa1Tk5n
[考博] 售SCI一区文章,我:8.O.55.1.O.54,科目齐全,可伽急 +3 qR2AdCWTVtkw 2026-10-09 7/350 2026-10-11 22:11 by XQNyDCa1Tk5n
[教师之家] 售SCI文章,我:8O5.5.1.O.54,科目齐全,可+急 (EPI+-1)(金币-50) +5 qR2AdCWTVtkw 2026-10-09 10/500 2026-10-11 22:06 by XQNyDCa1Tk5n
[教师之家] 售SCI一区T0P文章,我:8.O.55.1.O.5.4,科目全,可+急 (EPI+-1)(金币-50) +5 qR2AdCWTVtkw 2026-10-09 10/500 2026-10-11 14:25 by VOMexwnDzOFZ
[教师之家] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 (EPI+-1)(金币-50) +4 qR2AdCWTVtkw 2026-10-09 7/350 2026-10-11 14:05 by VOMexwnDzOFZ
[考研] 售SCI文章,我:8O.5.5.1O.54,科目全,可十急 +5 qR2AdCWTVtkw 2026-10-09 8/400 2026-10-11 13:51 by VOMexwnDzOFZ
[硕博家园] 售SCI-T0P文章,我:8O.5.5.1.O.54,科目齐全,可+急 +5 qR2AdCWTVtkw 2026-10-09 8/400 2026-10-11 13:45 by VOMexwnDzOFZ
[找工作] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +3 qR2AdCWTVtkw 2026-10-09 7/350 2026-10-11 10:54 by VOMexwnDzOFZ
[找工作] 售SCI一区T0P文章,我:8O.55.1.O.5.4,科目齐全,可+急 +4 qR2AdCWTVtkw 2026-10-09 7/350 2026-10-11 10:34 by VOMexwnDzOFZ
[考博] 招收博士生一名 +3 mandolin 2026-10-10 3/150 2026-10-11 10:21 by 不是山谷!
[公派出国] 售SCI一区T0P文章,我:8O.55.1.O.54,科目全,可伽急 +4 qR2AdCWTVtkw 2026-10-09 4/200 2026-10-11 03:14 by M0zrYVSAnxpE
[有机交流] 各位大佬,路线设计求助 +6 zapen 2026-10-08 8/400 2026-10-10 15:59 by zapen
[有机交流] 我应该选哪条合成路线让实验成功率高一些? +5 jing816y 2026-10-08 9/450 2026-10-10 09:05 by zapen
信息提示
请填处理意见