How to read some data from a text file

1 view (last 30 days)
Hello,
I have a text file like this:
#Parameters = {a=12.4; b=0.6; d=18; f_design=10; f_max=15; f_min=5; g=1; h=7.5; lambda=30; phi=0; react=-10000; theta=0}
#"Frequency / GHz" "S1(1),1(1) (react=-10000) [Re]" "S1(1),1(1) (react=-10000) [Im]" "Ref.Imp. [Re]" "Ref.Imp. [Im]"
#-------------------------------------------------------------------------------------------------------------------
10.000000000000 0.14429999628736 0.016848571131779 73.000000000000 0.00000000000000
#Parameters = {a=12.4; b=0.6; d=18; f_design=10; f_max=15; f_min=5; g=1; h=7.5; lambda=30; phi=0; react=-8000; theta=0}
#"Frequency / GHz" "S1(1),1(1) (react=-8000) [Re]" "S1(1),1(1) (react=-8000) [Im]" "Ref.Imp. [Re]" "Ref.Imp. [Im]"
#-----------------------------------------------------------------------------------------------------------------
10.000000000000 0.14004838730771 0.019270257352017 73.000000000000 0.00000000000000
I would like to read just the data that does not start with a " # ", therefore I would like to read:
10.000000000000 0.14429999628736 0.016848571131779 73.000000000000 0.00000000000000
10.000000000000 0.14004838730771 0.019270257352017 73.000000000000 0.00000000000000
How can I do that? because I cannot use the option of jumping the header due to the fact that those three lines that start with a # are repeated along all the file before the data I am interested in.
Thank you so much in advance,
Diego.

Accepted Answer

Star Strider
Star Strider on 9 Jul 2020
Use the textscan function:
fidi = fopen('TestFile20200709.txt','rt')
Datac = textscan(fidi, '%f%f%f%f%f', 'CommentStyle','#', 'CollectOutput',1)
fclose(fidi)
Data = cell2mat(Datac)
producing:
Data =
10.000000000000000 0.144299996287360 0.016848571131779 73.000000000000000 0.000000000000000
10.000000000000000 0.140048387307710 0.019270257352017 73.000000000000000 0.000000000000000
Defining the lines that begin with ‘#’ as comments tells textscan to ignore them.
I have also attached the file I created to test this code.
.
  4 Comments
Arthur Roué
Arthur Roué on 10 Jul 2020
Much more elegant than my solution, didn't know we can do that with textscan !
Star Strider
Star Strider on 10 Jul 2020
I learned much about MATLAB by participating in Answers, some of which involved my learning the many options textscan offers. I doubt I would have discovered them otherwise.

Sign in to comment.

More Answers (1)

Arthur Roué
Arthur Roué on 9 Jul 2020
Edited: Arthur Roué on 9 Jul 2020
Here one way to do this :
% File path
FilePath = fullfile(pwd, 'MyFile.txt');
% Open the file
FileID = fopen(FilePath, 'r');
if FileID == -1
% End of function because file couldn't be opened
error('File "%s" couldn''t be opened.\n', FilePath)
end
% Read all the lines
cLines = cell(1e4,1); % Pre-allocation
strLineInFile = fgetl(FileID); % 1st line
idxLine = 1;
while ischar(strLineInFile)
cLines{idxLine} = strLineInFile;
% Get next line
strLineInFile = fgetl(FileID);
idxLine = idxLine + 1;
end
fclose(FileID);
% Shorten the pre-allocated cell
nbLines = find(~cellfun(@ischar, cLines), 1, 'first') - 1;
cLines = cLines(1:nbLines);
% Detect line starting with '#'
vb = ~cellfun(@(line) strncmp(line, '#', 1), cLines);
% Convert data to numeric value
Values = cLines(vb);
% Values are separated with horizontal tab (\t)
Values = cellfun(@(line) str2double(strsplit(line, '\t')), Values, 'UniformOutput', 0);
Values = vertcat(Values{:});
Be aware that :
- any line starting with anythig but # will be interpret as data (and will be convert to numeric value)
- your separator on a line between value is a tab
If you change you text format, you'll have to adapt this script.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!