matlabtextscan

how to get the line number having a specific string in a txt file - matlab


I have a txt file that has a lot of content and in this file there is a lot of "include" word and I want to get data from all three lines after that.

myFile.txt: "-include:

-6.5 6.5

sin(x^2)

diff

-include

-5 5

cos(x^4)

diff"

How do I get this data in an array?


Solution

  • Based on your example (first include has : at the end, second one doesn't) you could use something like this.

    fID = fopen('myFile.txt'); % Open the file for reading
    textString = textscan(fID, '%s', 'Delimiter', '\n'); % Read all lines into cells
    fclose(fID); % Close the file for reading
    textString = textString{1}; % Use just the contents on the first cell (each line inside will be one cell)
    includeStart = find(contains(textString,'include'))+1; % Find all lines with the word include. Add +1 to get the line after
    includeEnd = [includeStart(2:end)-2; length(textString)]; % Get the position of the last line before the next include (and add the last line in the file)
    parsedText = cell(length(includeStart), 1); % Create a new cell to store the output
    % Loop through all the includes and concatenate the text with strjoin
    for it = 1:length(includeStart)
      parsedText{it} = strjoin(textString(includeStart(it):includeEnd(it)),'\n');
      % Display the output
      fprintf('Include block %d:\n', it);
      disp(parsedText{it});
    end
    

    Which results in the following output:

    Include block 1:
    -6.5 6.5
    sin(x^2)
    diff
    Include block 2:
    -5 5
    cos(x^4)
    diff
    

    You can tune the loop to suit your needs. If you just want line numbers, use includeStart and includeEnd variables.