thewildone26
thewildone26

Reputation: 11

Removing first 18 lines of text file in Matlab

I've been working on this assignment for my research project. I have a text file with the first 18 lines having the following information:

Person  Study, Run1102
Sex M
Born    
Code    4E8F-58F4-0F68

Record  07-10-2011 Preferred01
Application trplatf
Creation date   07/10/2011 16:33:24
Exercises   {
#   Name    Start,sec   Length,sec  Start time
1   Gait    0.00    90.04
}

Frequency,Hz    100.000
Count   9004

Time,ms Position,m  Butterfly.force,N   Butterfly.x,mm  Butterfly.y,mm
Calibr      

The rest of the information,, about 9000 lines, is formatted like:

0   0.022   62.07   92.43   0.00
10  0.045   0.00    NaN NaN
20  0.070   0.00    NaN NaN
30  0.096   0.00    NaN NaN

I need to figure out for one, how to load the file into Matlab properly, and then remove the 18 lines. I'm not sure if I should use a for loop, or a function within matlab.

Not sure what I have, but this is my code so far;

fid1=fopen('','rt');
fid2=fopen('formattedFile','wt');
nSkip=18; % number records; use input() if doing often and not fixed
for idx=1:nSkip
   l=fgetl(fid1);
end
% rest of file
while ~feof(fid1)
    l=fgetl(fid1);
    fprintf(fid2,'%s\n',l);
end
fid1=fclose(fid1);
fid2=fclose(fid2);

Any kind of guidance would help! I'm new at Matlab... So, sorry if the code I have makes no sense, I've been searching online for help.

Upvotes: 1

Views: 1537

Answers (1)

Cape Code
Cape Code

Reputation: 3574

You can read all the rows of your file like this:

fid = fopen(filename);
Rows = textscan(fid, '%s', 'delimiter', '\n');
fclose(fid);

which creates a cell array with the lines.

You can then scan the columns from the 19th row like this:

Columns= cellfun(@(x) textscan(x,'%f','delimiter','\t','CollectOutput',1), Rows{1,1}(19:end, :));

If the rows have a consistent size, you should be able to turn it into an array with cell2mat.

Upvotes: 1

Related Questions