在Matlab中读取复杂的长文本文件

在Matlab中读取复杂的长文本文件,matlab,text,text-files,Matlab,Text,Text Files,我有一个很长的文本文件,其中包含4个不同站点的数据,具有不同的时间步长: 1:00 station 1 a number 1 (e.g.0.6E-06) matrix1 (41x36) station 2 number 2 (e.g.0.1E-06) matrix2 (41x36) station 3 number 3 (e.g.0.2E-06) matrix3 (41x36) station 4 number 4 (e.g.0.4E-06) matrix4 (41x36)

我有一个很长的文本文件,其中包含4个不同站点的数据,具有不同的时间步长:

1:00
station 1
a number 1  (e.g.0.6E-06)
matrix1 (41x36)
station 2
number 2    (e.g.0.1E-06)
matrix2 (41x36)
station 3
number 3   (e.g.0.2E-06)
matrix3 (41x36)
station 4
number 4    (e.g.0.4E-06)
matrix4 (41x36)

2:00
station 1
a number   (e.g.0.24E-06)
matrix5 (41x36)
station 2
a number     (e.g.0.3E-06)
matrix6 (41x36)
station 3
number     (e.g.0.12E-06)
matrix7 (41x36)
station 4
number     (e.g.0.14E-06)
matrix8 (41x36)
。。。。。 等等

我需要按每个站点和每个步骤读取这些数据,并注意到每个矩阵应该通过与上面的数字相乘来缩放。例如:

你能帮忙吗


非常感谢。

我的想法是使用
fopen
textscan
读取文本文件。之后,您可以搜索关键字
FACTOR
的外观以细分输出。代码如下:

fid=fopen('example.txt'); % open the document
dataRaw=textscan(fid,'%s','Delimiter',''); % read the file with no delimiter to achieve a cell array with 1 cell per line of the text file
fclose(fid); % close the document
rows=cellfun(@(x) strfind(x,'FACTOR'),dataRaw,'uni',0); % search for appearances of 'FACTOR'
hasFactor=find(~cellfun(@isempty,rows{1})); % get rownumbers of the lines that contain the word FACTOR
dataRaw=dataRaw{1}; % convert array for easier indexing
for ii=1:(numel(hasFactor)-1) % loop over appearances of the word FACTOR
    array=cellfun(@str2num,dataRaw(hasFactor(ii)+2:hasFactor(ii+1)-1),'uni',0); % extract numerical data
    output{ii}=str2num(dataRaw{hasFactor(ii)+1})*cat(1,array{:}); % create output scaled by the factor
end
array=cellfun(@str2num,dataRaw(hasFactor(end)+2:end),'uni',0);
output{end+1}=str2num(dataRaw{hasFactor(end)+1})*cat(1,array{:}); % These last 2 lines add the last array to the ouput
outputMat=cat(3,output{:}); % convert to a 3-dimensional matrix
outputStations=[{output(1:4:end)} {output(2:4:end)} {output(3:4:end)} {output(4:4:end)}]; % Sort the output to have 1 cell for each station
outputColumnSums=cellfun(@(x) cellfun(@sum,x,'uni',0),outputStations,'uni',0); % To sum up all the columns of each matrix
outputRowSums=cellfun(@(x) cellfun(@(y) sum(y,2),x,'uni',0),outputStations,'uni',0);

这种方法非常慢,而且可能可以矢量化,但是如果你不需要快速,它应该可以完成这项工作。我创建了一个单元格输出,每个数组有一个单元格,一个三维数组作为可选输出。希望这对您没问题

我的想法是使用
fopen
textscan
读取文本文件。之后,您可以搜索关键字
FACTOR
的外观以细分输出。代码如下:

fid=fopen('example.txt'); % open the document
dataRaw=textscan(fid,'%s','Delimiter',''); % read the file with no delimiter to achieve a cell array with 1 cell per line of the text file
fclose(fid); % close the document
rows=cellfun(@(x) strfind(x,'FACTOR'),dataRaw,'uni',0); % search for appearances of 'FACTOR'
hasFactor=find(~cellfun(@isempty,rows{1})); % get rownumbers of the lines that contain the word FACTOR
dataRaw=dataRaw{1}; % convert array for easier indexing
for ii=1:(numel(hasFactor)-1) % loop over appearances of the word FACTOR
    array=cellfun(@str2num,dataRaw(hasFactor(ii)+2:hasFactor(ii+1)-1),'uni',0); % extract numerical data
    output{ii}=str2num(dataRaw{hasFactor(ii)+1})*cat(1,array{:}); % create output scaled by the factor
end
array=cellfun(@str2num,dataRaw(hasFactor(end)+2:end),'uni',0);
output{end+1}=str2num(dataRaw{hasFactor(end)+1})*cat(1,array{:}); % These last 2 lines add the last array to the ouput
outputMat=cat(3,output{:}); % convert to a 3-dimensional matrix
outputStations=[{output(1:4:end)} {output(2:4:end)} {output(3:4:end)} {output(4:4:end)}]; % Sort the output to have 1 cell for each station
outputColumnSums=cellfun(@(x) cellfun(@sum,x,'uni',0),outputStations,'uni',0); % To sum up all the columns of each matrix
outputRowSums=cellfun(@(x) cellfun(@(y) sum(y,2),x,'uni',0),outputStations,'uni',0);

这种方法非常慢,而且可能可以矢量化,但是如果你不需要快速,它应该可以完成这项工作。我创建了一个单元格输出,每个数组有一个单元格,一个三维数组作为可选输出。希望你没问题

我已经调查了你的情况,看来问题并不像预期的那样微不足道。请记住,如果我在数据位置的假设上犯了错误,您可以让我知道,以便我可以编辑它,或者您可以将数字更改为适合您的情况。在本例中,我最初将带分隔符的文件加载到Excel电子表格中,只是为了使其可视化

阅读之后,我发现可以指定要从
example.txt
中提取的确切行和列,如下所示:

data = dlmread('example.txt', ' ', [4 1 45 37]); % [r1 c1 r2 c2]
data2 = dlmread('example.txt', ' ', [47 1 88 37]);
其结果是两个矩阵
41-x-37
,仅包含数字。我在第4行开始
数据
,以绕过标题信息/字符串。注意到该模式,我将其设置为一个循环:

No_of_matrices_expected = 4;
dataCell = cell(No_of_matrices_expected, 1);
iterations = length(dataCell)

% Initial Conditions
rowBeginning = 4;
col1 = 1; % Constant
rowEnd = rowBeginning + 40; % == 44, right before next header information
col2 = 36; % Constant

for n = 1 : iterations
    dataCell{n} = dlmread('example.txt', ' ', [rowBeginning, col1, rowEnd, col2]);
    rowBeginning = rowBeginning + 41 + 2; % skip previous matrix and skip header info
    rowEnd = rowBeginning + 40;
end
然而,我偶然发现了你之前所说的,即有四个不同的电台,每个电台都有自己的时间戳。因此,运行该循环4次以上导致意外结果,MATLAB崩溃。原因是新的时间戳为日期创建了一个额外的行。现在,您可以更改上面的循环以补偿这一额外的行,或者您可以为每个站点创建多个
for
循环。这将是你的决定

现在,如果您想保存标题信息,我建议您查看一下。您可以简单地使用此函数将所有数据的第一列拉入字符串的单元格数组。然后,您可以拉出所需的标题信息。请记住,如果要使用
textscan
,请使用

我会让你使用到目前为止我发现的东西,但是如果你需要更多的帮助,请告诉我


数字

我已经调查了你的情况,似乎这个问题并不像预期的那样微不足道。请记住,如果我在数据位置的假设上犯了错误,您可以让我知道,以便我可以编辑它,或者您可以将数字更改为适合您的情况。在本例中,我最初将带分隔符的文件加载到Excel电子表格中,只是为了使其可视化

阅读之后,我发现可以指定要从
example.txt
中提取的确切行和列,如下所示:

data = dlmread('example.txt', ' ', [4 1 45 37]); % [r1 c1 r2 c2]
data2 = dlmread('example.txt', ' ', [47 1 88 37]);
其结果是两个矩阵
41-x-37
,仅包含数字。我在第4行开始
数据
,以绕过标题信息/字符串。注意到该模式,我将其设置为一个循环:

No_of_matrices_expected = 4;
dataCell = cell(No_of_matrices_expected, 1);
iterations = length(dataCell)

% Initial Conditions
rowBeginning = 4;
col1 = 1; % Constant
rowEnd = rowBeginning + 40; % == 44, right before next header information
col2 = 36; % Constant

for n = 1 : iterations
    dataCell{n} = dlmread('example.txt', ' ', [rowBeginning, col1, rowEnd, col2]);
    rowBeginning = rowBeginning + 41 + 2; % skip previous matrix and skip header info
    rowEnd = rowBeginning + 40;
end
然而,我偶然发现了你之前所说的,即有四个不同的电台,每个电台都有自己的时间戳。因此,运行该循环4次以上导致意外结果,MATLAB崩溃。原因是新的时间戳为日期创建了一个额外的行。现在,您可以更改上面的循环以补偿这一额外的行,或者您可以为每个站点创建多个
for
循环。这将是你的决定

现在,如果您想保存标题信息,我建议您查看一下。您可以简单地使用此函数将所有数据的第一列拉入字符串的单元格数组。然后,您可以拉出所需的标题信息。请记住,如果要使用
textscan
,请使用

我会让你使用到目前为止我发现的东西,但是如果你需要更多的帮助,请告诉我


数字

谢谢Max,但您的代码有一个错误:未定义的变量“rowInds”或类“rowInds”。感谢您的患者和帮助。如果我想按每个站点分离数据,这样看起来更容易,怎么样?我已经注意到:不能显示超过524288个元素的变量摘要。你是什么意思?您希望每个站都有一个变量吗?这将超过1000个变量你确定,你想要吗?您可以通过动态变量命名来实现这一点,这是非常糟糕的编程实践。但是你检查过细胞阵列的版本了吗?在输出变量中,每个站有一个单元格,您可以轻松地显示它。亲爱的Max,我的意思是我希望将数据站逐站分离为每个矩阵(变量),但每个矩阵(或变量)应涵盖所有时间步长。谢谢Max,但您的代码有一个错误:未定义的变量“rowInds”或类“罗林斯谢谢你的病人和帮助。如果我想按每个站点分离数据,这样看起来更容易,怎么样?我已经注意到:不能显示超过524288个元素的变量摘要。你是什么意思?您希望每个站都有一个变量吗?这将超过1000个变量你确定,你想要吗?您可以通过动态变量命名来实现这一点,这是非常糟糕的编程实践。但确实