Txt import in Matlab, different row formats

拥有回忆 提交于 2019-12-11 06:06:27

问题


I need to import variables from a txt file. This file has 3 main parts.

A) Initial headlines, containing general information

B) Headlines-Variables, in every column

C) Numerical data in every column

As below:

Headlines - Headlines - Headlines - Headlines
Headlines - Headlines - Headlines - Headlines


#    A      |      B              C      |      D        | 
# ----------+----------------------------+---------------|  
#    1      |  0.0000E+00  +  0.0000E+00 |    0.0000     |
#    2/3    |  0.0000E+00 +/- 0.0000E+00 |    0.0000     |
#    4/5    |  0.0000E+00 +/- 0.0000E+00 |    0.0000     |
#    6      |  0.0000E+00  +  0.0000E+00 |    0.0000     |

The problem is that the initial headlines are changing every time, so we cant declare a specific number of rows initially to avoid.

As you can see we have 2 different row formats. So we cant write a specific format for every line and the number of the numerical data in every column are changing also.

I cant do that (Data=textscan(fid,'%s %f %s %f %s %f %s %f', 'headlines', 4)

I have only two different types of row format

How can I import only the numerical data in every row.

Please HELP


回答1:


My favourite method is to read in the whole file with this magical command:

buf=textread(filename,'%s','delimiter','\n');

and then to parse it. In this case it seems easy to detect the data lines by looking for an initial #.




回答2:


You can apply textscan line by line instead of to the file as a whole. For example, based on the example you gave (and assuming you have written a function to determine the data format from the top lines):

fileID = fopen(fileName);
blockLine = 0;
while ~feof(fileID)
    currLine = fgetl(fileID);
    % Check if we've reached the datablock
    if strcmpi(currLine(1),'#')
       blockLine = blockLine + 1;
    end
    % Use first line of datablock to determine textscan format
    if blockLine == 1
        textFormat = [insert format determination function];
    elseif blockLine > 2
        % Ignoring second line (dashes only)
        lineData = textscan(currLine,textFormat);
        [insert code to distribute data to larger variables]
    end
end
fclose(fileID);


来源:https://stackoverflow.com/questions/17813666/txt-import-in-matlab-different-row-formats

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!