将一个大的、格式奇怪的数据文本文件读入MATLAB
需要读取一个巨大的文本文件,其中包含格式怪异的数据。格式如下:将一个大的、格式奇怪的数据文本文件读入MATLAB,matlab,text-files,cell-array,Matlab,Text Files,Cell Array,需要读取一个巨大的文本文件,其中包含格式怪异的数据。格式如下: //Header with Title Info //Header with Test1 Info //More Test1 Info 0,-156.875956035285 1.953125,-4.82866496038806 3.90625,-8.93502887648155 5.859375,-9.76964479822559 7.8125,-14.9767168331976 9.765625,-16.99490346720
//Header with Title Info
//Header with Test1 Info
//More Test1 Info
0,-156.875956035285
1.953125,-4.82866496038806
3.90625,-8.93502887648155
5.859375,-9.76964479822559
7.8125,-14.9767168331976
9.765625,-16.9949034672061
11.71875,-19.2709033739316
13.671875,-18.9948581866681
//Header with Test2 Info
//More Test2 Info
0,-156.875956035285
1.953125,-4.82866496038806
3.90625,-8.93502887648155
5.859375,-9.76964479822559
7.8125,-14.9767168331976
9.765625,-16.9949034672061
11.71875,-19.2709033739316
13.671875,-18.9948581866681
//Header with Test3 Info
//More Test3 Info
0,-156.875956035285
1.953125,-4.82866496038806
3.90625,-8.93502887648155
5.859375,-9.76964479822559
7.8125,-14.9767168331976
9.765625,-16.9949034672061
11.71875,-19.2709033739316
13.671875,-18.9948581866681
// End of Data
这是它的要点,除了每个标题下大约有25000个条目,而不是8个条目。我正在运行25个测试,需要将它们平均化为一组数据
基本上,我想按以下顺序解析数据:
function [ data ] = graph( input_args )
%Plot data
myData = fopen('mRoom_fSweep_25points_center.txt');
data = textscan(myData,'%s');
fclose(myData);
length(data)
end
我想我可以找到这个字符串数组的长度,并为整个操作列表计算出一个for循环,但我无法通过这一点:输出一直给我以下信息:
ans =
{772321x1 cell}
我不能用。当我尝试将其存储在变量中时,它给出的值为1。我这里缺少的单元阵列有什么奇怪的地方吗?我想您需要“测试信息”行中的信息 如果是这样,您需要使用两种不同的模式运行
textscan
:一种模式用于选择信息行,另一种模式用于读取数据:
info(1, end+1) = textscan(fid, '//%s','Delimiter', '');
data(1, end+1) = textscan(fid, '%f, %f', 'CollectOutput', true);
下面是我如何用循环和错误处理来包装它:
% [info, data] = read_data(file_name): Read a file in funky format
%
% info and data are cells of same size
function [info, data] = read_data(file_name)
[fid, msg] = fopen(file_name);
if fid<0
error('Unable to open file "%s": %s', file_name, msg);
end
% close the file no matter how we exit this funciton (error,
% ctrl-c,...)
finalize = onCleanup(@() fclose(fid));
info = cell(1,0);
data = cell(1,0);
while true
info(1, end+1) = textscan(fid, '//%s','Delimiter', '');
data(1, end+1) = textscan(fid, '%f, %f', 'CollectOutput', true);
if strcmpi(info{1,end}{end}, 'End of Data')
% End of data reached, exit here
info = info(1:end-1);
data = data(1:end-1);
break;
end
if isempty(data{1,end})
% Empty data, but not 'End of data' marker.
% Replace this error with break to accept files with missing
% "end of data" tags
error('Empty data before "End of Data" line')
end
end
end
我想您需要“测试信息”行中的信息 如果是这样,您需要使用两种不同的模式运行
textscan
:一种模式用于选择信息行,另一种模式用于读取数据:
info(1, end+1) = textscan(fid, '//%s','Delimiter', '');
data(1, end+1) = textscan(fid, '%f, %f', 'CollectOutput', true);
下面是我如何用循环和错误处理来包装它:
% [info, data] = read_data(file_name): Read a file in funky format
%
% info and data are cells of same size
function [info, data] = read_data(file_name)
[fid, msg] = fopen(file_name);
if fid<0
error('Unable to open file "%s": %s', file_name, msg);
end
% close the file no matter how we exit this funciton (error,
% ctrl-c,...)
finalize = onCleanup(@() fclose(fid));
info = cell(1,0);
data = cell(1,0);
while true
info(1, end+1) = textscan(fid, '//%s','Delimiter', '');
data(1, end+1) = textscan(fid, '%f, %f', 'CollectOutput', true);
if strcmpi(info{1,end}{end}, 'End of Data')
% End of data reached, exit here
info = info(1:end-1);
data = data(1:end-1);
break;
end
if isempty(data{1,end})
% Empty data, but not 'End of data' marker.
% Replace this error with break to accept files with missing
% "end of data" tags
error('Empty data before "End of Data" line')
end
end
end
不确定,但我认为在每个单元格元素中,您都保存了一行。您是否尝试使用数据{line,1}来处理这些条目?您必须使用花括号{}来获取单元格内的数据,而使用“普通”括号只能获取整个单元格。不确定,但我认为您在每个单元格元素中保存了一行。您是否尝试使用数据{line,1}来处理这些条目?你必须使用花括号{}来获取单元格内的数据,而“正常”括号只能获取整个单元格。这太不可思议了,我无法想象会有更彻底的响应,谢谢+1是一个很好的答案,但特别是用于使用
onCleanup
关闭文件。当尝试导入松散格式的文件时,这是一个很好的实践。这是难以置信的,我无法想象会有更彻底的响应,谢谢+1是一个很好的答案,但特别是用于使用onCleanup
关闭文件。在尝试导入格式松散的文件时,这是一个很好的做法。