MATLAB: Reading a certain portion of an ASCII file

asciitextscan

Hello,
I would like to read an ASCII file as the one attached.
the problem is that I want to save in a matrix only the columns relative to Time, X force, Y force, Z force and Magnitude.
Does anyone of you has some trick to do it?
Thank you so much

Best Answer

here you are
all the best
clear all
clc
% Read and display the file fgetl.m one line at a time:
% line#1 section# time x-force y-force z-force magnitude
% line#2 resultant moments x-moment y-moment z-moment magnitude
% line#3 centroids x y z area
% 1 0.00000E+00 0.0000E+00 0.0000E+00 0.0000E+00 0.0000E+00
a = ' 1 '; % string array to get appropriate line
fid = fopen('secforc_5.txt');
tline = fgetl(fid);
n = 0;
while ischar(tline)
% disp(tline) % will display all lines of txt file
if findstr(tline,a) % extract data only for targeted lines
n = n+1;
disp(n)
disp(tline)
% extract time,x-force,y-force,z-force,magnitude
out = str2num(tline);
time(n) = out(2);
x_force(n) = out(3);
y_force(n) = out(4);
z_force(n) = out(5);
magnitude(n) = out(6);
end
tline = fgetl(fid);
end
fclose(fid);
my_data = [time(:) x_force(:) y_force(:) z_force(:) magnitude(:)];
%save or export as ascii or csv file