`
deepfuture
  • 浏览: 4333616 次
  • 性别: Icon_minigender_1
  • 来自: 湛江
博客专栏
073ec2a9-85b7-3ebf-a3bb-c6361e6c6f64
SQLite源码剖析
浏览量:79428
1591c4b8-62f1-3d3e-9551-25c77465da96
WIN32汇编语言学习应用...
浏览量:68376
F5390db6-59dd-338f-ba18-4e93943ff06a
神奇的perl
浏览量:101498
Dac44363-8a80-3836-99aa-f7b7780fa6e2
lucene等搜索引擎解析...
浏览量:281206
Ec49a563-4109-3c69-9c83-8f6d068ba113
深入lucene3.5源码...
浏览量:14608
9b99bfc2-19c2-3346-9100-7f8879c731ce
VB.NET并行与分布式编...
浏览量:65559
B1db2af3-06b3-35bb-ac08-59ff2d1324b4
silverlight 5...
浏览量:31320
4a56b548-ab3d-35af-a984-e0781d142c23
算法下午茶系列
浏览量:45206
社区版块
存档分类
最新评论

matlab-textread指定单独输出变量的文件读入

 
阅读更多

text1.txt内容如下:

  货币资金,6865234.00 ,  短期借款,120000.00
  应收票据,72120.00 ,    应付票据,85500.00
  应收账款,38050.00 ,  应付账款,80200.00
    减:坏账准备,,  预收账款,
    应收账款净额,38050.00 ,  应付工资,
  其他应收款,,  应付福利费,8430.00
  预付账款,26600.00 ,  应交税金,24420.00
  存 货,281950.00 ,  应付股利,34000.00
  待摊费用,100.00 ,  其他应付款,
  流动资产合计,7284054.00 ,  预提费用,1600.00
>> help textread
 TEXTREAD Read formatted data from text file.
     A = TEXTREAD('FILENAME')
     A = TEXTREAD('FILENAME','',N)
     A = TEXTREAD('FILENAME','',param,value, ...)
     A = TEXTREAD('FILENAME','',N,param,value, ...) reads numeric data from
     the file FILENAME into a single variable.  If the file contains any
     text data, an error is produced.
 
     [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT')
     [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N)
     [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',param,value, ...)
     [A,B,C, ...] = TEXTREAD('FILENAME','FORMAT',N,param,value, ...) reads
     data from the file FILENAME into the variables A,B,C,etc.  The type of
     each return argument is given by the FORMAT string.  The number of
     return arguments must match the number of conversion specifiers in the
     FORMAT string.  If there are fewer fields in the file than in the
     format string, an error is produced.  See FORMAT STRINGS below for
     more information.
 
     If N is specified, the format string is reused N times.  If N is -1 (or
     not specified) TEXTREAD reads the entire file.
 
     If param,value pairs are supplied, user configurable options customize
     the behavior of TEXTREAD.  See USER CONFIGURABLE OPTIONS below.
 
     TEXTREAD works by matching and converting groups of characters from the
     file. An input field is defined as a string of non-whitespace
     characters extending to the next whitespace or delimiter character
     or until the field width is exhausted.  Repeated delimiter characters
     are significant while repeated whitespace characters are treated as
     one.
 
     FORMAT STRINGS
 
     If the FORMAT string is empty, TEXTREAD will only numeric data.
 
     The FORMAT string can contain whitespace characters (which are
     ignored), ordinary characters (which are expected to match the next
     non-whitespace character in the input), or conversion specifications.
 
     Supported conversion specifications:
         %n - read a number - float or integer (returns double array)
              %5n reads up to 5 digits or until next delimiter
         %d - read a signed integer value (returns double array)
              %5d reads up to 5 digits or until next delimiter
         %u - read an integer value (returns double array)
              %5u reads up to 5 digits or until next delimiter
         %f - read a floating point value (returns double array)
              %5f reads up to 5 digits or until next delimiter
         %s - read a whitespace separated string (returns cellstr)
              %5s reads up to 5 characters or until whitespace
         %q - read a double-quoted string, ignoring the quotes (returns cellstr)
              %5q reads up to 5 non-quote characters or until whitespace
         %c - read character or whitespace (returns char array)
              %5c reads up to 5 characters including whitespace
         %[...]  - reads characters matching characters between the
                   brackets until first non-matching character or
                   whitespace (returns cellstr)
                   use %[]...] to include ]
              %5[...] reads up to 5 characters
         %[^...] - reads characters not matching characters between the
                   brackets until first matching character or whitespace
                   (returns cellstr)
                   use %[^]...] to exclude ]
              %5[^...] reads up to 5 characters
 
     Note: Format strings are interpreted as with sprintf before parsing.
     For example, textread('mydata.dat','%s\t') will search for a tab not
     the character '\' followed by the character 't'.  See the Language
     Reference Guide or a C manual for complete details.
 
     Using %* instead of % in a conversion causes TEXTREAD to skip the
     matching characters in the input (and no output is created for this
     conversion).
 
     The % can be followed by an optional field width to handle fixed
     width fields. For example %5d reads a 5 digit integer. In
     addition the %f format supports the form %<width>.<prec>f.
 
     USER CONFIGURABLE OPTIONS
 
     Possible param/value options are:
          'bufsize'      - maximum string length in bytes (default is 4095)
          'commentstyle' - one of
               'matlab'  -- characters after % are ignored
               'shell'   -- characters after # are ignored
               'c'       -- characters between /* and */ are ignored
               'c++'    -- characters after // are ignored
          'delimiter'    - delimiter characters (default is none)
          'emptyvalue'   - empty cell value in delimited files (default is 0)
          'endofline'    - end of line character (default determined from file)
          'expchars'     - exponent characters (default is 'eEdD')
          'headerlines'  - number of lines at beginning of file to skip
          'whitespace'   - whitespace characters (default is ' \b\t')
    
     TEXTREAD is useful for reading text files with a known format.  Both
     fixed and free format files can be handled.
 
     Examples:
      Suppose the text file mydata.dat contains data in the following form:
         Sally    Type1 12.34 45 Yes
         Joe      Type2 23.54 60 No
         Bill     Type1 34.90 12 No
          
      Read each column into a variable
        [names,types,x,y,answer] = textread('mydata.dat','%s%s%f%d%s');
 
      Read first column into a cell array (skipping rest of line)
        [names]=textread('mydata.dat','%s%*[^\n]')
 
      Read first character into char array (skipping rest of line)
        [initials]=textread('mydata.dat','%c%*[^\n]')
 
      Read file as a fixed format file while skipping the doubles
        [names,types,y,answer] = textread('mydata.dat','%9c%5s%*f%2d%3s');
 
      Read file and match Type literal
        [names,typenum,x,y,answer]=textread('mydata.dat','%sType%d%f%d%s');
 
      Read m-file into cell array of strings
        file = textread('fft.m','%s','delimiter','\n','whitespace','');
 
      To read all numeric data from a delimited text file, use a single output
      argument, empty format string, and the appropriate delimiter. For
      example, suppose data.csv contains:
        1,2,3,4
        5,6,7,8
        9,10,11,12
 
      Read the whole matrix into a single variable:
        [data] = textread('data.csv','','delimiter',',');
 
      Read the first two columns into two variables:
        [col1, col2] = textread('data.csv','%n%n%*[^\n]','delimiter',',');
 
      For files with empty cells, use the emptyvalue parameter.  Suppose
      data.csv contains:
        1,2,3,4,,6
        7,8,9,,11,12
 
      Read the file like this, using NaN in empty cells:
        [data] = textread('data.csv','','delimiter',',','emptyvalue',NaN);
 
    The TEXTSCAN function is intended as a replacement for both STRREAD and
    TEXTREAD.

 

 

 >> [name1,value1,name2,value2]=textread('e:\test1.txt','%s %f %s %f','delimiter',',')

name1 =

    '  货币资金'
    '  应收票据'
    '  应收账款'
    '减:坏账准备'
    '应收账款净额'
    '  其他应收款'
    '  预付账款'
    '  存 货'
    '  待摊费用'
    '  流动资产合计'


value1 =

     6865234
       72120
       38050
           0
       38050
           0
       26600
      281950
         100
     7284054


name2 =

    '  短期借款'
    '应付票据'
    '  应付账款'
    '  预收账款'
    '  应付工资'
    '  应付福利费'
    '  应交税金'
    '  应付股利'
    '  其他应付款'
    '  预提费用'


value2 =

      120000
       85500
       80200
           0
           0
        8430
       24420
       34000
           0
        1600

>>

读取第一行

>> [name1,value1,name2,value2]=textread('e:\test1.txt','%s %f %s %f',1,'delimiter',',')

name1 =

    '  货币资金'


value1 =

     6865234


name2 =

    '  短期借款'


value2 =

      120000

>>

 

前2行

>> [name1,value1,name2,value2]=textread('e:\test1.txt','%s %f %s %f',2,'delimiter',',')

name1 =

    '  货币资金'
    '  应收票据'


value1 =

     6865234
       72120


name2 =

    '  短期借款'
    '应付票据'


value2 =

      120000
       85500

>>

 

分享到:
评论

相关推荐

    Matlab中的textread和textscan函数

    介绍了Matlab中常用的textread和textscan函数的使用,通过实例演示文件文本的读取

    Reading-text-files-in-Matlab.rar_fscanf_matlab textread_text rea

    Using file io functions in Matlab is a bit of confusion due to the variety and the issue of vectorisation. In the following snippet data is read back using: fscanf : creates a vector row by row ...

    matlab.rar_textread_坐标 txt_读取XYZ

    如何读取数据textread,例如通过xin textread( x_inlet.txt )我们可以把文件x_inlet.txt中的数据存储到数组xin中,且xin的维数和大小会自动与文件x_inlet.txt的相匹配。 如何设定坐标轴范围和坐标间距axis,set 例如 ...

    textread函数用法大全

    matlab经常要读取数据。使用textread函数,可以方便的读取各种形式并不一致的文件。

    textread解析样例

    matlab中textread的样例 基本各种用法都有,自己试验过,可以通过运行。适合初学者,或者调用函数的工程师。

    MATLAB实现txt文本数据分离

    MATLAB实现txt文本数据分离 部分源码 clear;clc A=textread('1.txt','%s'); %读入原始的文本,请查看变量A的结构 k=1; for i=1:2:length(A) %从A中把字符串提取出来 text{k}=A{i}; k=k+1; end

    big_textread:加载具有无限数量的列和行的 ASCII 文件。-matlab开发

    此函数读取几乎所有大小的文本文件。 对行数和克隆数没有限制,因为您知道文件中的克隆数。 此功能对于读取麻烦的 ASCII 文件最有用。

    readMM_2D:读取使用 Mathematica 'Put' 命令保存的二维矩阵。-matlab开发

    如果未指定行数,或者 TXT2MAT 不可用,则该函数使用 TEXTREAD 读取文件并自动确定输入数据的行数和列数。 输入: 文件——字符串变量; 完全指定的文件名nrows -- 数值标量; 数据文件中的行数flagstr -- 'fast' ...

    Matlab_SVM.zip_CVX_UCI 数据集_cvx_begin_matlab CVX工具箱_散点图 优化

    1.读取数据:在Matlab中调用textread可读取UCI数据集,这里读取的文件是iris.data,因为文件中以逗号为分隔符,所以还要在读取方法中添加参数“‘delimiter’,‘,’”,从而在读数据的时候自动跳过分隔符。...

    matlabsin函数源码-Tricks4MATLAB:收集的技巧

    matlab sin函数源码 MATLAB Notes by WENQI 该笔记记录本人在科研过程中所学到的MATLAB实用操作,为方便查阅总结如下,代码都经过本人验证实用,由于来源广泛,未能列全参考文献。 与系统交互 直接使用bat命令 ...

    textread读写

    放大法放大人gtr而他热突然突然饿啊特哇特爱饿哇reward饿傲天饿啊饿啊

    readrinexobs2.rar

    读obsdata.txt的时候用matlab的textread()一个一个的读,然后根据epoch.txt知道每秒多少个卫星,由于每个卫星有8个数据,所以按照8个一循环读取。KMIT2000.20o文件到第37681秒数据之前都是好的,该行之后文件格式...

    matlab最简单的代码-mvlsa:多视图LSA

    从该集合中下载文件,或者如果您决定使用其他嵌入文件,请注意,matlab文件中的嵌入矩阵需要与词汇表对齐,然后进行标准化,如下面的代码所示。 word=textread('$(VOCAB_500K_FILE)', '%s'); load('$(EM

    pcloud2mat:将点云超级简单地转换为 .mat 文件中的 lat,lon,z 向量-matlab开发

    并不真正需要它自己的功能,但它使使用“textread”更容易。

    autotextread:读取数据文件; 自动检测 hdr 中的列名和格式。-matlab开发

    textread() 的包装器,它会自动为您生成格式字符串(每列都是数字或字符串); 将数据放入一个结构体,其中的字段以文件标题行给出的列名命名。 已使用 36 列数据文件。 主要基于解决方案编号 26207,但这需要您知道...

    基于MATLAB的车牌字符识别源码+项目说明.zip

    将字符图像进行二值化操作,将图像上的像素点的灰度值设置为0或255,也就是将整个图像呈现出明显的黑白效果的过程,而在Matlab中,一幅二值图像是一个取值只有0和1的逻辑数组。通常做法是先把彩色图像转化为灰度图像...

    File Exchange 一周:(减少到下载统计)-matlab开发

    文件 fex.txt 具有“高”布局,可以使用 [t,id,rank,file,fcat,down] = textread('fex.txt',' s\t%d\t%d\t%d\t%d\t%d','delimiter' ,'\n'); time = datenum(t,'dd-mmm-yyyy HH:MM:SS'); 其中 id 和 rank 是 FEX ...

    可以在windows上成功运行的DPM release 4.0版本

    1.修改globals.m中的一些全局变量(主要是目录设定) cachedir= 'D:\DPMtrain\VOCCache\'; % 训练好的模型结果和中间数据的文件目录,此目录可以自己任意指定 tmpdir ='D:\DPMtrain\VOCtemp\'; % 训练中用到的临时文件...

    电力系统稳态潮流程序

    潮流程序代码,MATLAB,仿真计算,电力系统稳态。电力系统暂态。电力系统方向。function []= fromY1(filename); clc;%清除指令窗口 tic; data=textread('4.txt'); %%读取系统参数 nodeNum=data(1,1); lineNum=data...

    TE化工过程数据集

    TE process数据集 包含21个类别的训练数据和测试数据 内容非常全面,内容格式为.dat文件,可使用textread函数直接导入到matlab中

Global site tag (gtag.js) - Google Analytics