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

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后二进制输出到文本文件

    在MATLAB中,将源文件读入并以二进制格式输出到文本文件是一个常见的数据处理操作,尤其在处理大量数据或需要与不同编程语言交换数据时。这个过程包括两个主要步骤:首先读取源文件,然后以二进制格式写入到新的文本...

    matlab-函数手册大全.rar_MATLAB 函数大全_Matlab函数_matlab 函数_matlab函数大全_matl

    MATLAB函数可以大致分为基本数学函数、数组和矩阵操作、绘图函数、数据处理与分析、控制流和逻辑函数、文件输入输出、系统接口等几大类。例如,`sin`、`cos`等是基础数学函数,用于三角运算;`zeros`、`ones`生成...

    derdikman-Dordek-et-al.-Matlab-code.zip

    很抱歉,但根据提供的文件信息,“derdikman-Dordek-et-al.-Matlab-code.zip”似乎是一个包含Matlab代码的压缩包,而压缩包内的文件名称列表却是一系列工作总结和计划的文档,这二者之间并没有直接关联。Matlab是一...

    matlab-基础知识.zip

    在命令窗口中输入命令进行计算,工作空间显示变量及其值,历史窗口记录了之前输入的命令,而文件浏览器则帮助我们管理MATLAB脚本和函数。 二、数据类型与变量 MATLAB支持多种数据类型,包括数值型(如单精度float、...

    MATLAB读入数据的几种方式探讨.pdf

    1. 文本文件读入:使用fscanf函数或textread函数从文本文件中读入数据。 2. 二进制文件读入:使用fread函数从二进制文件中读入数据。 3. 数据库读入:使用database函数从数据库中读入数据。 三、MATLAB中读入数据...

    matlab-class课程作业.zip

    - **变量与数据类型**:MATLAB支持多种数据类型,如标量、向量、矩阵、复数、字符串等。理解这些基本数据结构对于编写有效的MATLAB代码至关重要。 - **运算符与表达式**:MATLAB支持算术、关系、逻辑等多种运算符...

    Matlab-code-TGCN-Mar2018-master_Will_unicode_

    2. **读写文件**:当处理包含非ASCII字符的文件时,需要在`textread`, `textwrite`, 或其他文件操作函数中指定`'-encoding'`选项,如`'-encoding','utf8'`,以确保正确解析和保存Unicode字符。 3. **兼容性**:在跨...

    matlab开发-读写日志文件

    在MATLAB开发中,日志文件的读写是常见的任务,尤其在数据分析、调试和监控等场景中。Roehrig Engineering Shock 5.x是一款专业软件,用于模拟和分析冲击加载情况,它生成的日志文件可能包含了大量的实验数据和计算...

    matlab开发-显示文本文件

    首先,MATLAB提供了一个内置函数`textread`,可以用来读取文本文件的内容。例如,如果你有一个名为`data.txt`的文本文件,你可以用以下代码读取文件的全部内容: ```matlab fid = fopen('data.txt', 'r'); content ...

    matlab中读取txt数据文件

    ### MATLAB中读取TXT数据文件的方法 在MATLAB中处理文本数据时,经常会遇到需要从TXT文件中读取数据的情况。这些TXT文件可能只包含数值数据、或者混合了中文字符和数字等多种情况。本文将详细介绍如何根据不同类型...

    Matlab中的textread和textscan函数

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

    MATLAB编程-输入输出函数.pdf

    在本节中,我们将详细介绍MATLAB中的输入输出函数,包括文本读取、文件操作、二进制I/O函数和格式化I/O函数等。 文本读取函数 MATLAB提供了多种文本读取函数,以读取文本文件中的数据。其中,textread函数是其中之...

    Matlab - Primer

    - 读写文件:用load、save、textread、csvwrite等函数处理各种数据格式。 - 数据接口:与Excel、数据库和其他软件交换数据。 8. **类与对象**: -面向对象编程:定义类,创建对象,实现继承和封装。 - 工具箱:...

    第二节.zip_CST-Matlab联合仿真_checkcx9_cst_cst matlab_matlab cst

    7. **CST与MATLAB的数据交换**:为了实现两者之间的通信,可能需要编写MATLAB脚本来读取和解析CST的输出文件,或者利用CST提供的专用MATLAB接口,如CST MATLAB Link,它可以简化数据交互过程。 8. **优化流程**:在...

    code_matlab_文本文件筛选输出_

    这个场景中提到的"code_matlab_文本文件筛选输出_"是一个与MATLAB相关的项目,目标是根据用户指定的气象站代码,从大型文本文件中筛选出相应气象站的信息并进行输出。MATLAB是一种强大的编程环境,尤其适合数值计算...

    matlab中数据文件的处理

    `load` 命令用于从文件中加载变量,通常无需指定文件类型即可自动识别。若希望强制使用某种格式加载文件,可以通过 `-ASCII` 或 `-MAT` 参数实现。 #### 2. `fopen` 和 `fclose` `fopen` 命令用于打开文件以便读取...

    matlab开发-文件夹目录列表

    这个“matlab开发-文件夹目录列表”可能是一个示例或教程,旨在教授如何使用MATLAB获取指定文件夹下的所有文件和子文件夹的列表。下面我们将详细探讨这一主题。 首先,MATLAB中的`dir`函数是用于获取目录信息的关键...

    Matlab-achieve-Huffman-decoding.zip_huffman_huffman decoding_huf

    在提供的文件"Matlab 中实现哈夫曼编译码.doc"中,详细描述了如何在Matlab环境下实现这些步骤。它可能包括了具体的代码示例,解释了如何用Matlab的语法来处理频率表、构建哈夫曼树、生成和解码编码等。通过学习这份...

    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 ...

Global site tag (gtag.js) - Google Analytics