【问题标题】:reading a text file to MATLAB with different formatting使用不同格式将文本文件读取到 MATLAB
【发布时间】:2010-02-11 00:30:10
【问题描述】:

我有一个文本文件,但不幸的是它的格式很差,但是我想将文本文件的内容读入一个矩阵,但我不知道该怎么做。

当尝试使用fscanftextscantextread 和其余部分时,它只会将所有内容复制到一个单元格中,但我不希望这样。

这是内容的样子:所以我只想读取小数而不是绝对数字。谁能帮帮我。

 1 : 13.27 ;  3 : 20.68 ;  6 : 8.271 ;  7 : 3.308 ;  8 : 8.328 ; 
 9 : 6.655 ;  10 : 16.58 ;  11 : 9.925 ;  12 : 12.41 ;  13 : 4.135 ; 
 14 : 9.925 ;  15 : 11.58 ;  16 : 10.87 ;  17 : 1.654 ;  18 : 4.962 ; 
 19 : 6.655 ;  22 : 10.98 ;  23 : 24.25 ;  24 : 47.33 ;  25 : 11.6 ; 
 26 : 9.925 ;  27 : 5.809 ;  28 : 5.001 ;  29 : 6.617 ;  30 : 7.577 ; 
 31 : 9.155 ;  32 : 7.444 ;  33 : 28.58 ;  34 : 9.155 ;  35 : 35.83 ; 

【问题讨论】:

  • 我或许可以帮助你,但我不确定你想要什么。你能给出给定输入所需的输出吗?或者您可以为较小的输入提供所需的输出吗?
  • 另外,你是在什么环境下运行的?您可以访问 Perl、Python 等吗?我了解您使用的是 MATLAB,但使用更合适的工具将数据处理成 MATLAB 的 ascii 矩阵格式,然后以通常的方式加载它可能要简单得多。

标签: matlab file-io


【解决方案1】:

只使用 textscan 并忽略不需要的东西,比如数字和 : 给你一个非常简单的解决方案:

fid = fopen('test.txt', 'rt');
data = textscan(fid, '%*u %*1s %f', 'Delimiter', ';');
fclose(fid);

将 test.txt 更改为您的文件名。 data 是一个包含双打的单元格。

>> data{:}

ans =

   13.2700
   20.6800
    8.2710
    3.3080
    8.3280
    6.6550
   16.5800
    9.9250
   12.4100
    4.1350
    9.9250
   11.5800
   10.8700
    1.6540
    4.9620
    6.6550
   10.9800
   24.2500
   47.3300
   11.6000
    9.9250
    5.8090
    5.0010
    6.6170
    7.5770
    9.1550
    7.4440
   28.5800
    9.1550
   35.8300

【讨论】:

    【解决方案2】:

    我假设冒号 (:) 分隔一行中的值,分号 (;) 分隔行。在此假设下,以下函数应将您的数据读入 MATLAB 矩阵

    function dat = readData(filename)
    % FUNCTION dat = readData(filename)
    % Reads data from a nonstandard formatted file, filename
    % INPUTS:
    %    filename:  Full path to data file with the following format
    %                      1 : 2; 3 : 4
    %               where the : separates values in a row and ; separates rows
    
    % open the file for reading
    fid = fopen(filename);
    
    ii=0; % row index
    
    while ~feof(fid) % loop until we find the end of the file
        str = fgetl(fid); % get a line of text
    
        while 1
            % split the string into its component parts
            % assume that values are split with colon ":" and
            % rows are identified with a
            % semicolon ";".
            % 
            % split at the first row using strtok
            [rowStr rem]=strtok(str,';');
    
            % split rowStr using colon
            [str1,str2]=strtok(rowStr,':');
    
            % get rid of the colon in str2
            str2 = strrep(str2,':',' ');
    
            str1 =strtrim(str1);
            str2 =strtrim(str2);
    
            % store data if we found any
            if ~isempty(str1)&& ~ isempty(str2)
                ii=ii+1; % increment row index
                dat(ii,:) = [str2double(str1) str2double(str2)];
            end
    
            if isempty(rem), break; end
            str = rem(2:end);
        end
    end
    fclose(fid);
    

    您可以使用函数 round、floor、ceil 来提取“十进制值”。

    【讨论】:

      【解决方案3】:

      概念上:

      取字符串。

      改变 : 和 ;带空格和 \n (返回)带 ;

      将字符串赋值给一个变量,这个变量应该变成一个矩阵。

      使用双循环更改每个元素的值

      var(i,j)=floor(var(i,j)) - var(i,j);
      

      你在这里(如果我没有误解你的话)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-05-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-12-07
        相关资源
        最近更新 更多