【问题标题】:Matlab:Reading textfile into matrixMatlab:将文本文件读入矩阵
【发布时间】:2014-08-18 17:20:58
【问题描述】:

我想在 Matlab 中从文本文件中读取数据。该文件描述了一个表面由两部分组成: 1) 包含 x、y 尺寸(以像素和 nm 为单位)等信息的标题。 2)高度值矩阵(512x512)

当我首先尝试仅读取矩阵时,我最终得到的是单列向量而不是矩阵 C = textscan(id,'%f','HeaderLines',18,'Delimiter','\t') 该文件包含“”分隔值和分隔矩阵行的行。当我手动删除标题并阅读它时,它可以工作 A=dlmread(path,'\t',[0 0 511 511]);

我想用 textscan 获得相同的 512x512 矩阵,以便我以后也可以解析标题 - 我该怎么做?

【问题讨论】:

    标签: matlab textscan


    【解决方案1】:

    如果您没有严格绑定到textscan,则可以改用fscanf(请参阅docs)。像这样:

    fid = fopen('FileName.txt');
    
    % here you have to skip (or parse) your header lines
    % and determine number of rows/columns in your data matrix
    
    A = fscanf(fid, '%g', [NColumns NRows]);
    fclose(fid);
    
    % Transpose so that A matches
    % the orientation of the file
    A = A';
    

    更新1

    此代码可能不是很优雅,但它会将您的标头解析为 FileInfo 结构,因此您可以稍后在代码中使用这些数据。

    % sample file header
    %
    % # File Format = ASCII
    % # Created by SPIP 6.2.8.0 2014-08-15 20:41
    % # Original file: D:\...
    % # x-pixels = 512
    % # y-pixels = 512
    % # x-length = 319375
    % # y-length = 319375
    % # x-offset = 0
    % # y-offset = 0
    % # z-unit = [nm]
    % # scanspeed = 638.75
    % # forcecurve = 0
    % # voidpixels =76
    % # description =62:Confocal Height Image  Date: 2013-08-20T13:36 User: Unknown
    % # Start of Data: MATRIX
    
    fid = fopen('text.txt','r');
    
    FileInfo  = [];
    tmpString = '';   % initializing the s
    while isempty(strfind(tmpString,'Start of Data: MATRIX'))
    
        % we read a string from file
        tmpString = fgetl(fid);
        % and split it into left and right parts according to the '=' position
        [kev,val] = strtok( tmpString(3:end) , ':=' );
    
        % remove spaces from key name and make it a valid variable name in MatLab
        keyName = genvarname( strrep(strrep(kev,'-',''),' ','') );
    
        % check if key value is a number
        [x,OK]    = str2num(strtrim(val(2:end)));
    
        % if value is a number, use it otherwise leave the trimmed original string
        if OK
            FileInfo.(keyName) = x;
        else
            FileInfo.(keyName) = strtrim(val(2:end));
        end
    
    end
    
    % Now we can read the data
    A = fscanf(fid, '%g', [FileInfo.xpixels FileInfo.ypixels]);
    fclose(fid);
    
    % Transpose so that A matches
    % the orientation of the file
    %A = A';
    

    【讨论】:

    • 谢谢 - 它有效。但是我在解析头部时遇到了问题。有没有办法解析文档的前 n 行?或者解析它直到字符串“数据:”?我阅读了有关 formatSpec 的文档,但由于某种原因我没有正确理解。
    • 一切都取决于标题的形成方式,没有一般规则。你能举个例子吗?
    • # File Format = ASCII # Created by SPIP 6.2.8.0 2014-08-15 20:41 # Original file: D:\... # x-pixels = 512 # y-pixels = 512 # x-length = 319375 # y-length = 319375 # x-offset = 0 # y-offset = 0 # z-unit = [nm] # scanspeed = 638.75 # forcecurve = 0 # voidpixels =76 # description =62:Confocal Height Image Date: 2013-08-20T13:36 User: Unknown # Start of Data: MATRIX 我只需要 x&y 尺寸,然后再进入 Matrix。 (每个#都换行,stackoverflow刚刚改了)
    • 非常感谢。 fgetl 是我一直在寻找的解决方案,也感谢您对解析实现的帮助!
    猜你喜欢
    • 2015-01-30
    • 1970-01-01
    • 2015-02-16
    • 1970-01-01
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 2013-01-22
    • 1970-01-01
    相关资源
    最近更新 更多