Mark K
Mark K

Reputation: 43

read Mutli-Format data file in MATLAB

I have a data file that looks likes this:

some_words
some words | more words
1 2 3 4
1 2 3 4
1 2 3 4


some_other_words
some words | moar words
1 2 3 4
1 2 3 4
1 2 3 4

What I would like to do is read in only the numbers, ignoring the line breaks and text in the file without having to use fgetl and iterate line by line. if I do:

data = textscan(fileId,'%f%f%f%f','CellectOutput',false,'Headerlines',2)

The data reads in the first block of text but stops when it gets to the line break. The line break is ALWAYS 2 lines long. Not sure if that helps out at all.

Upvotes: 0

Views: 36

Answers (1)

Georgggg
Georgggg

Reputation: 564

You can use :

id = fopen('fgetl.m');

tline = fgetl(fid);
while ischar(tline)
    disp(tline)
    tline = fgetl(fid);
end

fclose(fid);

to read the entire file, or:

clc;
fid = fopen('fileName.m');
sl = 2;
for i=1:sl
  header = fgetl(fid);
  disp(strcat("Header ignored: ",header));
end

while ischar(tline)
    disp(strcat("Line imported: ",tline))
    tline = fgetl(fid);
end

fclose(fid);

to ignore the first 2 lines and import the rest, you should save or do whatever you want with the value of "tline" variable inside the while loop. Important CHANGE the value of "sl" variable for the number of lines you want to ignore in your file. It works for any plain text file.

Upvotes: 2

Related Questions