简体   繁体   English

八度/ Matlab-读取固定宽度的文件

[英]Octave / Matlab - Reading fixed width file

I have a fixed width file format (original was input for a Fortran routine). 我有一个固定宽度的文件格式(原始输入是用于Fortran例程的)。 Several lines of the file look like the below: 文件的几行如下所示:

1078.0711005.481 932.978 861.159 788.103 716.076

How this actually should read: 实际内容应为:

1078.071 1005.481 932.978 861.159 788.103 716.076

I have tried various methods, textscan, fgetl, fscanf etc, however the problem I have is, as seen above, sometimes because of the fixed width of the original files there is no whitespace between some of the numbers. 我尝试了各种方法,例如textscan,fgetl,fscanf等,但是如上所述,我遇到的问题是,有时由于原始文件的宽度固定,所以某些数字之间没有空格。 I cant seem to find a way to read them directly and I cant change the original format. 我似乎找不到直接读取它们的方法,也无法更改原始格式。

The best I have come up with so far is to use fgetl which reads the whole line in, then I reshape the result into an 8,6 array 到目前为止,我想出的最好的方法是使用fgetl读取整行,然后将结果重塑为8,6数组

A=fgetl
A=reshape(A,8,6)

which generates the following result 产生以下结果

11
009877
703681
852186
......
049110
787507
118936

So now I have the above and thought I might be able to concatenate the rows of that array together to form each number, although that is seeming difficult as well having tried strcat, vertcat etc. 所以现在我有了上述内容,并认为我可以将数组的行连接在一起以形成每个数字,尽管尝试strcat,vertcat等似乎也很困难。

All of that seems a long way round so was hoping for some better suggestions. 所有这些似乎还有很长的路要走,因此希望有一些更好的建议。

Thanks. 谢谢。

If you can rely on three decimal numbers you can use a simple regular expression to generate the missing blanks: 如果可以依靠三个十进制数字,则可以使用一个简单的正则表达式来生成缺失的空格:

s = '1078.0711005.481 932.978 861.159 788.103 716.076';
s = regexprep(s, '(\.\d\d\d)', '$1 ');
c = textscan(s, '%f');

Now c{1} contains your numbers. 现在c{1}包含您的数字。 This will also work if s is in fact the whole file instead of one line. 如果s实际上是整个文件而不是一行,那么这也将起作用。

You haven't mentioned which class of output you needed, but I guess you need to read doubles from the file to do some calculations. 您没有提到所需的输出类别,但我想您需要从文件中读取double值以进行一些计算。 I assume you are able to read your file since you have results of reshape() function already. 我假设您已经可以使用reshape()函数来读取文件了。 However, using reshape() function will not be efficient for your case since your variables are not fixed sized (ie 1078.071 and 932.978). 但是,由于您的变量的大小不是固定的(即1078.071和932.978),因此使用reshape()函数在您的情况下效率不高。

If I did't misunderstand your problem: 如果我没有误解您的问题:

  1. Your data is squashed in some parts (ie 1078.0711005.481 instead of 1078.071 1005.481). 您的数据在某些部分被压缩(即1078.0711005.481代替1078.071 1005.481)。

  2. Fractional part of variables have 3 digits. 变量的小数部分为3位数字。

First of all we need to get rid of spaces from the string array: 首先,我们需要去除字符串数组中的空格:

A = A(~ismember(A,' '));

Then using the information that fractional parts are 3 digits: 然后使用小数部分为3位的信息:

iter = length(strfind(A, '.'));
for k=1:iter   
    [stat,ind] = ismember('.', A);
    B(k)=str2double(A(1:ind+3));
    A = A(ind+4:end); 
end

B will be an array of doubles as a result. B将是一个双精度数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM