我有一个包含数字和大量文本行的文本文件。但我只需要在 Matlab 中将数字读取为两个单独的矩阵。文件是这样的:
finite element method
Node Number
1 2 3
1 3 4
2 3 4
coordinates:
10 20
0 20
20 20
14 0
我有一个包含数字和大量文本行的文本文件。但我只需要在 Matlab 中将数字读取为两个单独的矩阵。文件是这样的:
finite element method
Node Number
1 2 3
1 3 4
2 3 4
coordinates:
10 20
0 20
20 20
14 0
fid=fopen('filename.txt'); %opening the file
while ~feof(fid) %reading up to end of the file
tline = fgetl(fid); %reading line by line
if strcmp(tline, 'Node Number') %finding the line containing " Node Number"
break %stop reading if the "Node Number" was detected
end
end
Nnum =transpose( fscanf(fid,'%d ' , [3 Inf])) %reading the lines after the line which we were stop
% reading would stop as soon
% as the format of reading
% doesn't match anymore
while ~feof(fid) %continuing reading line by line again
tline = fgetl(fid);
if strcmp(tline, 'coordinates:')%stop when the line "coordinates"
was detected
break
end
end
Coordinate =transpose( fscanf(fid,'%d ' , [2 Inf])) %reading lines after line
"coordinate"