Matlab文件IO--文本与数值的读

来源:互联网 发布:html个人主页简单源码 编辑:程序博客网 时间:2024/05/29 05:00

   最近项目仿真需要用到Matlab的GUI设计,与所有其他设计语言GUI设计一样,最基础也是最重要的就是有关数据的读写以及数据类型的转换。所以今天就对Matlab文件IO进行下学习,这篇重点是文本与数值读的应用。

   1. 读取纯数值的文本文件

    

%% 读取纯数值的文本文件format long gdat1 = load('data1.txt');dat2 = importdata('data1.txt')[a,b,c,d] = textread('data1.txt','%2d %8.3f %8.3f %7.3f') %使用textread函数                                                          %读取该文件到4个列向量中fid = fopen('data1.txt');  %以只读模式打开该文本文件,为fscanf和textscan函数                            %的读取操作做准备dat3 = fscanf(fid,'%g',[4,inf]); %读取4*inf个elementsfrewind(fid);          %将文本指针移到文件开头dat4 = textscan(fid,'%2d %8.3f %8.3f %7.3f'); %采用textscan函数读取文本文件的数值                                                %到单元数组中dat4{:} %查看该单元数组fclose(fid);fid = fopen('data1.txt','rt');   %以文本模式打开该文件dat5 = fread(fid);  %读取该文本的所有字符,返回其ASCII值                    %reads                    %data from a binary file into column列 vector A and                    %positions the file pointer at the end-of-file markerstr5 = char(dat5')   %将ASCII值转化为字符串dat6 = str2num(str5)              %由字符串获取数值数组fclose(fid);

   2. 读取包含文本和数值的文本文件


%% 读取包含文本和数值的文本文件dat1 = importdata('data2.txt',' ',1); %loads                                       %data from ASCII file, filename, or the clipboard,                                        %reading numeric data starting from line headerlinesIn+1                                        %这里指明文本是以空格来作为分隔的,如果是文本以逗号分隔                                        %这里就需要改为','dat1.data        %dai1为一个结构体,返回其数值段dat1.textdata    %返回dat1的文本数据段,即第一行和第一列。非数值项都存储在该段内[a, b, c, d] = textread('data2.txt','%s %f %f %f','headerlines',1) %采用textread函数                                                                   %函数读取该文件,跳过第一行fid = fopen('data2.txt');   %以制度模式打开dat2 = textscan(fid,'%s %f %f %f','HeaderLines',1)dat2{:} %以列的形式展示fclose(fid);

  3. 读取包含文本和数据混合的文本文件


%% 读取包含文本和数据混合的文本文件dat1 = textread('data3.txt','%s','delimiter','\n')  %将\n换行视为分隔符                                                     %When textread reads a consecutive series                                                     %of whitespace values, it treats them as one white                                                     %space. When it reads a consecutive series of delimiter values,                                                     %it treats each as a separate delimiter.n1 = str2num(dat1{3})              %提取第三行的数值,即为串口1的帧数,将字符串单元转换为数值型dat1_1 = dat1(6:9)                 %提取串口1的数据到字符串单元数组dat1_1中dat1_2 = cell2mat(deblank(dat1_1))    %将字符串单元数组转换为字符数组,                                      %removes all trailing whitespace and                                       %null characters from the end of character string strdat1_3 = str2num(dat1_2)           %将字符数组转换为数值数组para1 = dat1_3(:,2)                %提取串口1的参数2的值,即数组数组dat1_3的第二列m = find(strcmp(dat1,'串口2帧数:'))  %找到内容为“串口2帧数:”的行n2 = str2num(dat1{m+1})             %提取串口2的帧数dat2_1 = dat1(m+4:m+4+n2)          %提取串口2的数据到字符串单元数组dat2_1中dat2_2 = cell2mat(deblank(dat2_1))  %将字符串单元数组转换为字符数组dat2_3 = str2num(dat2_2)            %将字符数组转换为数值数组%%%%%%%%%%%%%%%%%%%%%%以下程序段采用textscan函数读取文本文件并提取数据%%%%%%%%%%%%%%%%%%%%%%%%fid = fopen('data3.txt');           %以只读模式打开n1 = textscan(fid,'%d','HeaderLines',2)    %采用textscan读取两行之后的数据,即为串口1的帧数dat1 = textscan(fid,'%[^串口]','HeaderLines',2)   %从当前位置跳两行开始读取字符串                                                 %直到遇到“串口”等字符dat1_1 = str2num(dat1{:}{:})        %将上面读到的字符串转化为数值数组。注意,dat1_1为包含回车符的字符串fclose(fid);


3的结果输出:

dat1 = 


    '串口数据以混合格式存储示例'
    '串口1帧数:'
    '4'
    '串口1数据:'
    '时间        参数1    参数2   参数3'
    '00:00:00:000  0.000325 0.000378 0.000598'
    '00:00:00:040  0.000256 0.000245 0.000698'
    '00:00:00:080  0.000369 0.000251 0.000651'
    '00:00:00:120  0.000372 0.000249 0.000648'
    ''
    '串口2帧数:'
    '3'
    '串口2数据:'
    '时间     参数1     参数2 '
    '00:00:00:000  0.000325 0.000378'
    '00:00:00:040  0.000256 0.000245'
    '00:00:00:080  0.000369 0.000251'
    '00:00:00:120  0.000372 0.000249 '




n1 =


     4




dat1_1 = 


    '00:00:00:000  0.000325 0.000378 0.000598'
    '00:00:00:040  0.000256 0.000245 0.000698'
    '00:00:00:080  0.000369 0.000251 0.000651'
    '00:00:00:120  0.000372 0.000249 0.000648'




dat1_2 =


00:00:00:000  0.000325 0.000378 0.000598
00:00:00:040  0.000256 0.000245 0.000698
00:00:00:080  0.000369 0.000251 0.000651
00:00:00:120  0.000372 0.000249 0.000648




dat1_3 =


   1.0e-03 *


    0.3250    0.3780    0.5980
    0.2560    0.2450    0.6980
    0.3690    0.2510    0.6510
    0.3720    0.2490    0.6480




para1 =


   1.0e-03 *


    0.3780
    0.2450
    0.2510
    0.2490




m =


    11




n2 =


     3




dat2_1 = 


    '00:00:00:000  0.000325 0.000378'
    '00:00:00:040  0.000256 0.000245'
    '00:00:00:080  0.000369 0.000251'
    '00:00:00:120  0.000372 0.000249 '




dat2_2 =


00:00:00:000  0.000325 0.000378
00:00:00:040  0.000256 0.000245
00:00:00:080  0.000369 0.000251
00:00:00:120  0.000372 0.000249




dat2_3 =


   1.0e-03 *


    0.3250    0.3780
    0.2560    0.2450
    0.3690    0.2510
    0.3720    0.2490




n1 = 


    [4]




dat1 = 


    {1x1 cell}




dat1_1 =


   1.0e-03 *


    0.3250    0.3780    0.5980
    0.2560    0.2450    0.6980
    0.3690    0.2510    0.6510
    0.3720    0.2490    0.6480

0 0