zoukankan      html  css  js  c++  java
  • Matlab数据导入导出

    转载:http://www.cnblogs.com/thisismyth/archive/2008/11/25/1340891.html

    Matlab提供了从磁盘文件或剪贴簿转载数据至工作区(数据导入)和将工作区变量存入磁盘文件(数据导出)的多种途径。

    最简单的办法是使用界面导入向导,打开文件菜单中的导入数据而后按提示操作。

    一、导入文本文件

    load函数、dlmread函数

    文本文件需要具备统一的行列模式,使用分隔符作为数据项间隔,这些分隔符包括空格、逗号、tab、分号或其它。数据文件可能附带标题行和行列头标签。

    数值数据

    对于数值数据可以直接使用load函数装载,例如my_data.txt中数据如下:

    1 2 3 4 5

    6 7 8 9 10

    命令A = load('my_data.txt')装载该文本文件数据。

     

    如果数值数据使用其它分隔符,可以使用dlmread读入,假设my_data.txt中数据如下:

    7.2;8.5;6.2;6.6

    5.4;9.2;8.1;7.2

    命令A = dlmread('my_data.txt', ';')读入该数据。

    包含行列标签的数值数据

    例如:

    Grade1 Grade2 Grade3

    78.8 55.9 45.9

    99.5 66.8 78.0

    89.5 77.0 56.7

    fid = fopen('grades.dat', 'r');

    grades = textscan(fid, '%f %f %f', 3, 'headerlines', 1);

    fclose(fid);

    包含字符和数值的混合数据

    使用textread函数读入。

    如果是规则的用空格隔开的数据,则采用data=textread(filename)格式调用,读出的数据记录在data矩阵中。

    二、导出文本文件

    save函数

    A = [ 1 2 3 4 ; 5 6 7 8 ];

    save my_data.out A –ASCII

    dlmwrite函数

    dlmwrite('my_data.out',A, ';')

    三、MS-Excel电子表格文件

    xlsinfo获得文件信息

    使用命令[type, sheets] = xlsfinfo(filename)返回文件类型type和工作表信息。如:[type, sheets] = xlsfinfo('tempdata.xls')

    Xlswrite导出数据

    d = {'Time', 'Temp'; 12 98; 13 99; 14 97}

    命令xlswrite('tempdata.xls', d, 'Temperatures', 'E1')将单元格数组d的数据写出至tempdata.xls文件,新建工作表'Temperatures',从该工作表的E1单元格开始写入。

    Xlsread读入数据

    ndata = xlsread('tempdata.xls', 'Temperatures')

    [ndata, headertext] = xlsread('tempdata.xls', 'Temperatures')

    底层文件输入输出函数

    fclose   关闭文件

    fopen   打开文件

    fread   从文件中读入二进制数据

    fwrite  把二进制数据写入文件

    fgetl   逐行从文件中读取数据并放弃换行符

    fgets   从文件中读取行,保留换行符并把行作为字符串返回

    fprintf  把格式化数据写入文件

    fscanf  从文件中读取格式化数据

    feof    测试文件是否结束

    ferror   测试文件输入输出错误信息

    frewind 文件指针归零

    fseek   设置文件位置指针

    ftell    获取文件位置指针

    sprintf  把格式化数据写入一个字符串

    sscanf   使用格式控制读取字符串

    底层文件输入输出函数-->特殊函数

    csvread   读取逗号分隔格式的数据文件到矩阵

    csvwrite   写矩阵到逗号分隔格式的数据文件

    dlmread   把一个ASCII限定文件(数据文件)读入矩阵

    dlmwrite   把矩阵写入到ASCII限定文件(数据文件)

    hdf       HDF接口??

    imfinfo   返回图形图象文件的信息

    imread    读取图象(到矩阵)

    imwrite   写入图象

    textread   从文本文件读取格式化数据(important)

    wk1read  把Lotus123电子表格读入矩阵

    wk1write  把矩阵写入Lotus123wk1电子表格

    xlsread   读取excel表格


     Example 1 — Reading Different Types of DataText file scan1.dat contains data in the following
    form:

    Sally  Level1 12.34 45 1.23e10 inf NaN Yes

    Joe    Level2 23.54 60 9e19 -inf 0.001 No

    Bill   Level3 34.90 12 2e5 10 100 No

    Read each column into a variable: 

    fid = fopen('scan1.dat');
         C = textscan(fid, '%s %s %f32 %d8 %u %f %f %s');
         fclose(fid);

    Note:Spaces between the conversion specifiers are shown only to make the example easier to read. They are not required.

    textscan returns a 1-by-8 cell array C with the following cells:

    C{1} = {'Sally'; 'Joe'; 'Bill'}          class cell
         C{2} = {'Level1'; 'Level2'; 'Level3'}    class cell
         C{3} = [12.34; 23.54; 34.9]              class single
         C{4} = [45; 60; 12]                      class int8
         C{5} = [4294967295; 4294967295; 200000]  class uint32
         C{6} = [Inf; -Inf; 10]                   class double
         C{7} = [NaN; 0.001; 100]                 class double
         C{8} = {'Yes'; 'No'; 'No'}               class cell

    The first two elements of C{5} are the maximum values for a 32-bit unsigned integer, or intmax('uint32').

  • 相关阅读:
    core dump的使用
    wav文件格式
    Unicode编码 【转】
    WAV格式中常见的压缩编码
    两台交换机级联端口mac地址表维护(转载)
    pthread_cond_wait()的使用方法
    makefile自动依赖[转]
    11月的第一天
    再读simpledb 之 事务管理的实现(3)
    再读simpledb 之 元数据管理(1)
  • 原文地址:https://www.cnblogs.com/CodeWorkerLiMing/p/12007708.html
Copyright © 2011-2022 走看看