Domanda

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

È stato utile?

Soluzione

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 #.

Altri suggerimenti

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);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top