Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/160.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab:将文本文件读入矩阵_Matlab_Textscan - Fatal编程技术网

Matlab:将文本文件读入矩阵

Matlab:将文本文件读入矩阵,matlab,textscan,Matlab,Textscan,我想从Matlab中的文本文件中读取数据。该文件描述的曲面由两部分组成: 1) 带有x、y尺寸信息(以像素和nm为单位)的标题。 2) 带高度值的矩阵(512x512) 当我试图先读取矩阵时,我得到的是一个列向量,而不是矩阵 C=textscan(id,'%f','headerline',18,'Delimiter','t') 该文件包含“”分隔的值和分隔矩阵行的行。当我手动删除标题并使用 A=dlmread(路径,'\t',[0 0 511 511]) 我想通过textscan获得相同的512

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

当我试图先读取矩阵时,我得到的是一个列向量,而不是矩阵
C=textscan(id,'%f','headerline',18,'Delimiter','t')
该文件包含“”分隔的值和分隔矩阵行的行。当我手动删除标题并使用
A=dlmread(路径,'\t',[0 0 511 511])


我想通过textscan获得相同的512x512矩阵,以便以后也可以解析标题-我如何才能做到这一点?

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

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行?或者解析它直到字符串“Data:”?我阅读了formatSpec上的文档,但由于某些原因,我没有理解正确。一切都取决于标题的形成方式,没有一般规则。你能提供一个例子吗?
#File Format=ASCII#由SPIP 6.2.8.0 2014-08-15 20:41创建#原始文件:D:\…#x像素=512#y像素=512#x长度=319375#y长度=319375#x偏移量=0#y偏移量=0#z单元=[nm]#扫描速度=638.75#力曲线=0#空洞像素=76#描述=62:共聚焦高度图像日期:2013-08-20T13:36用户:未知#数据开始:矩阵大小:我只需要得到矩阵的x和y。(每个都在一个新的行中,stackoverflow刚刚更改了它)非常感谢。fgetl是我一直在寻找的解决方案,也感谢您对解析实现的帮助!