Matlab 在Octave 4.0.3(300万行/250 MB文件)上使用textscan()时出现问题

Matlab 在Octave 4.0.3(300万行/250 MB文件)上使用textscan()时出现问题,matlab,octave,Matlab,Octave,我正试图重写一段MATLAB代码,以便它可以使用倍频程运行,但我发现使用textscan()函数有一些问题 原始代码(MATLAB): 样本数据: # U POINT_DATA 3711396 # x y z U_x U_y U_z 739263.5 9363820 172.809998 -5.34212399 -0.0408997531 0.0736143066 739263.5 9363789 172.979996 -5.34212399 -0.0408997531 0.0

我正试图重写一段MATLAB代码,以便它可以使用倍频程运行,但我发现使用textscan()函数有一些问题

原始代码(MATLAB):

样本数据:

# U  POINT_DATA 3711396
#  x  y  z  U_x  U_y  U_z  
739263.5 9363820 172.809998 -5.34212399 -0.0408997531 0.0736143066
739263.5 9363789 172.979996 -5.34212399 -0.0408997531 0.0736143066
739294.312 9363820 172.449997 -5.34212399 -0.0408997531 0.0736143066
739294.312 9363789 173.710007 -5.34212399 -0.0408997531 0.0736143066
739325.125 9363820 170.699997 -5.248474 -0.00403332808 0.041700209
739325.125 9363789 172.350006 -5.37227834 -0.0307070923 0.0492642202
739355.938 9363820 168.690002 -5.248474 -0.00403332808 0.041700209
739355.938 9363789 170.5 -5.37227834 -0.0307070923 0.0492642202
739386.75 9363820 169.110001 -5.248474 -0.00403332808 0.041700209
739386.75 9363789 170.839996 -5.37227834 -0.0307070923 0.0492642202
739417.562 9363820 170.789993 -5.248474 -0.00403332808 0.041700209
739417.562 9363789 171.820007 -5.37227834 -0.0307070923 0.0492642202
我已经尝试过使用其他函数,例如dlmread()、load()甚至fgetl()来完成这项工作,但与使用MATLAB时使用的8s相比,它花费的时间太多了

用“%f%f%f%f%f%f”替换formatSpec也不起作用

该文件包含3711396行和250MB的数据,分为六列数据


你能帮我修改一下代码吗?

我仔细检查了你的代码,发现有两件事阻碍了它的运行

第一个是在formatSpec中使用
%[^\n\r]

第二个是使用“ReturnOneError”名称/值对

这两个特性在八度音阶中还不受支持

我能够使用以下修改后的代码成功导入您提供的示例数据:

function data = import_file(filename, startRow, endRow)

if nargin<=2
    startRow = 3;
    endRow = inf;
end

formatSpec = '%f%f%f%f%f%f';
% Corrected formatSpec to import 6 consecutive floats 

fileID = fopen(filename,'r');

dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1,...
    'EmptyValue' ,NaN,...
    'HeaderLines', startRow(1)-1); 
% Removed 'ReturnOnError' as it is not yet implimented in Octave.

for block=2:length(startRow)
    frewind(fileID);

    dataArrayBlock = textscan(fileID, formatSpec,...
        endRow(block)-startRow(block)+1,...
        'EmptyValue' ,NaN,...
        'HeaderLines', startRow(block)-1);

    for col=1:length(dataArray)
        dataArray{col} = [dataArray{col};dataArrayBlock{col}];
    end
end

fclose(fileID);

data = [dataArray{1:end}]; 
%Changed 'end-1' to 'end' to include last column.

end
function data=import\u文件(文件名、开始行、结束行)

如果使用
加载
应该是以八度读取数据的最快方式。您是否尝试过加载(文件名)(startRow:endRow,:)
# U  POINT_DATA 3711396
#  x  y  z  U_x  U_y  U_z  
739263.5 9363820 172.809998 -5.34212399 -0.0408997531 0.0736143066
739263.5 9363789 172.979996 -5.34212399 -0.0408997531 0.0736143066
739294.312 9363820 172.449997 -5.34212399 -0.0408997531 0.0736143066
739294.312 9363789 173.710007 -5.34212399 -0.0408997531 0.0736143066
739325.125 9363820 170.699997 -5.248474 -0.00403332808 0.041700209
739325.125 9363789 172.350006 -5.37227834 -0.0307070923 0.0492642202
739355.938 9363820 168.690002 -5.248474 -0.00403332808 0.041700209
739355.938 9363789 170.5 -5.37227834 -0.0307070923 0.0492642202
739386.75 9363820 169.110001 -5.248474 -0.00403332808 0.041700209
739386.75 9363789 170.839996 -5.37227834 -0.0307070923 0.0492642202
739417.562 9363820 170.789993 -5.248474 -0.00403332808 0.041700209
739417.562 9363789 171.820007 -5.37227834 -0.0307070923 0.0492642202
function data = import_file(filename, startRow, endRow)

if nargin<=2
    startRow = 3;
    endRow = inf;
end

formatSpec = '%f%f%f%f%f%f';
% Corrected formatSpec to import 6 consecutive floats 

fileID = fopen(filename,'r');

dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1,...
    'EmptyValue' ,NaN,...
    'HeaderLines', startRow(1)-1); 
% Removed 'ReturnOnError' as it is not yet implimented in Octave.

for block=2:length(startRow)
    frewind(fileID);

    dataArrayBlock = textscan(fileID, formatSpec,...
        endRow(block)-startRow(block)+1,...
        'EmptyValue' ,NaN,...
        'HeaderLines', startRow(block)-1);

    for col=1:length(dataArray)
        dataArray{col} = [dataArray{col};dataArrayBlock{col}];
    end
end

fclose(fileID);

data = [dataArray{1:end}]; 
%Changed 'end-1' to 'end' to include last column.

end