Matlab-读取具有不同行长的文件 [英] Matlab - read file with varying line lengths

查看:98
本文介绍了Matlab-读取具有不同行长的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据文件,每行有不同数量的数据,我想将它们作为数组加载到Matlab中.例如,假设数据文件看起来像

I have a data file with varying amount of data per line that I would like to load into Matlab as an array. As an example, suppose the data file looks like

1 2
3 4 5 6
7
8 9 10

我想将其作为一个看起来像数组的数组读入Matlab

I want to read it into Matlab as an array that looks like

1  2  nan nan
3  4   5   6
7 nan nan nan
8  9  10  nan

我可以通过在文件的所有行上进行for循环来做到这一点,但是我的文件很大,我正在寻找一种有效的解决方案.任何想法将不胜感激.如果有帮助,我也知道文件中最大行长的上限.

I can do this by doing a for loop over all lines of the file but my files are very large and I am looking for an efficient solution. Any ideas would be highly appreciated. If it helps, I also know an upper bound on the maximum line length across the file.

推荐答案

如果您的文本文件中没有零值,则Divakar的答案有效,但通常情况并非如此.例如,如果您的文本数组是

While Divakar's answer works if you don't have any values of zero in your text file, that's may not generally be the case. For example, if your text array was

1 2 3
4 0
5 6 0 7 8

那么Divakar的结果将是:

then Divakar's result would be:

1 2 3 nan nan
4 nan nan nan nan
5 6 nan 7 8

您真正想要的地方:

1 2 3 nan nan
4 0 nan nan nan
5 6 0 7 8

最简单的方法是打开dlmread函数(只需在文本编辑器中键入dlmread并按Ctrl + D即可将其打开).确保将此文件另存为您使用其他名称(即dlmread_nan.m)所在的目录中的单独文件.

The easiest way to implement this is to open up the dlmread function (just type dlmread into the text editor and press Ctrl+D to open it up). Make sure to save this file as a separate file in the directory you're working in with a different name (i.e. dlmread_nan.m).

转到代码的这一部分(我的版本中的第126行):

Go down to this part of the code (line 126 in my version):

if isempty(delimiter)
    result  = textscan(fid,'',nrows,'headerlines',r,'headercolumns',c,...
                       'returnonerror',0,'emptyvalue',0, 'CollectOutput', true);
else
    delimiter = sprintf(delimiter);
    whitespace  = setdiff(sprintf(' \b\t'),delimiter);
    result  = textscan(fid,'',nrows,...
                   'delimiter',delimiter,'whitespace',whitespace, ...
                   'headerlines',r,'headercolumns',c,...
                   'returnonerror',0,'emptyvalue',0,'CollectOutput', true);
end

,然后将'emptyvalue'之后的值都更改为NaN而不是0.保存文件.它应该看起来像这样:

and change the value after 'emptyvalue' in both cases to NaN instead of 0. Save the file. It should look like this:

if isempty(delimiter)
    result  = textscan(fid,'',nrows,'headerlines',r,'headercolumns',c,...
                       'returnonerror',0,'emptyvalue',NaN, 'CollectOutput', true);
else
    delimiter = sprintf(delimiter);
    whitespace  = setdiff(sprintf(' \b\t'),delimiter);
    result  = textscan(fid,'',nrows,...
                   'delimiter',delimiter,'whitespace',whitespace, ...
                   'headerlines',r,'headercolumns',c,...
                   'returnonerror',0,'emptyvalue',NaN,'CollectOutput', true);
end

要获取阵列,请使用此:

To get your array, use this:

result = dlmread_nan('text.txt', ' '); 
%%//This will give you exactly what you're looking for.

这有点麻烦,但是通过从MATLAB的库中进行复制,它可能比从头开始编写它更加健壮且没有错误.

It's a little cumbersome, but by copying from MATLAB's library, it'll probably be a lot more robust and error-free than writing it from scratch yourself.

这篇关于Matlab-读取具有不同行长的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆