2013-06-18 1 views
0

숫자와 숫자가 많은 텍스트 파일이 있습니다. 하지만 Matlab에서 두 개의 별도 Matrix로 숫자를 읽어야합니다. 파일은 다음과 같다 :Matlab에서 텍스트 읽기

finite element method 
Node Number 
1 2 3 
1 3 4 
2 3 4 
coordinates: 
10 20 
0 20 
20 20 
14 0 
+0

당신이 파일 형식에 대한 좀 더 명확한 될 수 있을까? 파일의 행 "노드 번호"및 "좌표 :"부분이 있습니까? 아니면 두 개의 별도 파일이 있습니까? 숫자가있는 줄 사이에 빈 줄이 있습니까? –

+0

안녕하세요, 먼저 관심을 가져 주셔서 감사합니다. 모두 같은 파일에 있으며 숫자 사이에 빈 줄이 없습니다. 사실 내 문제가 어떻게 든 해결되었습니다. 내가 작성한 코드는 이것이었다. 더 좋은 아이디어가 있습니까 ??? –

답변

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"