Reading a file and want to stop when the column ends

2 views (last 30 days)
I am collecting the informaiton from the first row of a set of columns from a notepad file.
It reads the file, but the while loop doesnt stop nor does it continue to the next line as I want it to.
I also dont know how many rows there are so I dont know an appropriate condition to give the while loop for it to stop when the row of numbers stop.
for example I just want the bolded numbers.
ex:
3651 4007 4015 2 0.560
3652 4015 4023 2 0.274
NODAL COORDINATES
*****************
NODE X-COORD Y-COORD Z-COORD
(mm) (mm) (mm)
...
fid_tr = fopen('Struct.S3E');
line_in_tr = fgets(fid_tr);
lineinfo_tr = sscanf(line_in_tr,'%f');
stp_crit_tr = ' TRUSS ELEMENTS';
stop_tr = contains(line_in_tr,stp_crit_tr);
while stop_tr == 0
line_in_tr = fgets(fid_tr);
stop_tr = contains(line_in_tr,stp_crit_tr);
end
for i = 1:1:5
line_in_tr = fgets(fid_tr);
end
long_mat = 0;
tie_mat = 0;
long_elmnts = [];
tie_elmnts = [];
t=1;
r=1;
node_per_layer = 64;
while isnumeric(lineinfo_tr(1))== 1 %This line is the issue
if isempty(long_elmnts) ==1
if abs(lineinfo_tr(2) - lineinfo_tr(3)) == node_per_layer
long_mat = lineinfo_tr(4);
long_elmnts(t) = lineinfo_tr(4);
t=t+1;
line_in_tr = fgets(fid_tr);
end
end
if isempty(tie_elmnts) ==1
if abs(lineinfo_tr(2) - lineinfo_tr(3)) ~= node_per_layer
tie_mat =lineinfo_tr(4);
tie_elmnts(r) = lineinfo_tr(4);
r=r+1;
line_in_tr = fgets(fid_tr);
end
end
end

Accepted Answer

Star Strider
Star Strider on 30 Aug 2020
Perhaps textscan can do what you want:
fid_tr = fopen('Struct.S3E');
c = textscan(fid_tr '%f%*f%*f%*%*f')
cd= cell2mat(c);
fclose(fid_tr);
This reads the first value in the field, ignoring the others. See the documentation for specific name-value pair arguments to customise it to do what you want.
It would likely heop to have your file (or a representative sample if it is too large) to work with.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!