将文件加载到 Matlab 中的效率
Efficiency loading a file into Matlab
我正在尝试在 Matlab 中加载文件。但是我对最好的方法有点困惑。
该文件有 3 列,如下面的屏幕截图所示:
我可以通过 load('c')
.
快速加载此文件
但是,我必须在底行添加 2 个 NaN。
原始文件实际上看起来像下面的文件:
现在,如果我对下面的文件执行 load('c')
,我会收到错误消息:
Error using load
Unable to read file 'c'. Input must be a MAT-file or an ASCII file containing numeric
data with same number of columns in each row.
当然我可以使用 ImportData 导入这个文件,但是导入它太慢了。
有什么建议吗?
您应该可以使用 c = readtable('c')
。默认情况下,这应该会自动将空条目更改为“NaN”,但如果没有,可以在选项中进行设置。
如果我有一个难以导入的文件(在 readtable()
之前......这在过去几年使事情变得容易得多),我会经常使用导入数据工具(如果它一个非常大的文件,你可以制作一个复杂文件的模型,这样它加载速度更快)然后根据需要更改所有导入设置,然后在绿色复选标记为“导入选择”的地方使用黑色下拉箭头到 select “生成函数。”这将为您提供设置所有内容的编码方式,以按照您想要的方式获取文件。
load()
更适合读取以前保存的在 Matlab 中创建的“.mat”文件。
这是一种低级方法,可能比其他方法更快:
filename = 'c'; % name of the file
N = 3; % number of columns
fid = fopen(filename, 'r'); % open file for reading
x = fscanf(fid, '%f'); % read all values as a column vector
fclose(fid); % close file
x = [x; NaN(N-mod(numel(x)-1,N)-1, 1)]; % include NaN's to make length a multiple of N
x = reshape(x, N, []).'; % reshape to N columns in row-major order
我正在尝试在 Matlab 中加载文件。但是我对最好的方法有点困惑。
该文件有 3 列,如下面的屏幕截图所示:
我可以通过 load('c')
.
但是,我必须在底行添加 2 个 NaN。
原始文件实际上看起来像下面的文件:
现在,如果我对下面的文件执行 load('c')
,我会收到错误消息:
Error using load
Unable to read file 'c'. Input must be a MAT-file or an ASCII file containing numeric
data with same number of columns in each row.
当然我可以使用 ImportData 导入这个文件,但是导入它太慢了。
有什么建议吗?
您应该可以使用 c = readtable('c')
。默认情况下,这应该会自动将空条目更改为“NaN”,但如果没有,可以在选项中进行设置。
如果我有一个难以导入的文件(在 readtable()
之前......这在过去几年使事情变得容易得多),我会经常使用导入数据工具(如果它一个非常大的文件,你可以制作一个复杂文件的模型,这样它加载速度更快)然后根据需要更改所有导入设置,然后在绿色复选标记为“导入选择”的地方使用黑色下拉箭头到 select “生成函数。”这将为您提供设置所有内容的编码方式,以按照您想要的方式获取文件。
load()
更适合读取以前保存的在 Matlab 中创建的“.mat”文件。
这是一种低级方法,可能比其他方法更快:
filename = 'c'; % name of the file
N = 3; % number of columns
fid = fopen(filename, 'r'); % open file for reading
x = fscanf(fid, '%f'); % read all values as a column vector
fclose(fid); % close file
x = [x; NaN(N-mod(numel(x)-1,N)-1, 1)]; % include NaN's to make length a multiple of N
x = reshape(x, N, []).'; % reshape to N columns in row-major order