How to add a new line in the middle of a text file?
    조회 수: 19 (최근 30일)
  
       이전 댓글 표시
    
Hi all,
I have a text file in which I need to add a new line of text using Matlab. I know the number of the line, where the line should be inserted. However, I don't know how to create and fill a new line at this position. Can any of you help me out?
Thanks, Ellen
댓글 수: 0
채택된 답변
  per isakson
      
      
 2014년 12월 19일
        
      편집: per isakson
      
      
 2014년 12월 19일
  
      There is no "smart" way. One has to read the existing file and write the new. If the file isn't too large this could be a starting point.
    fid = fopen( old_filespec );
    cac = textscan( fid, '%s', 'Delimiter','\n', 'CollectOutput',true );
    fclose( fid )
    fid = fopen( new_filespec, 'w' );
    for jj = 1 : insert_here
        fprintf( fid, '%s\n', cac{jj} );
    end
    fprintf( fid, '%s\n', new_line );
    for jj = insert_here+1 : length(cac)
        fprintf( fid, '%s\n', cac{jj} );
    end
    fclose( fid );
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
				Help Center 및 File Exchange에서 Data Import and Export에 대해 자세히 알아보기
			
	제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!