How to replace some lines in a file with lines of another file?

조회 수: 1 (최근 30일)
Kona
Kona 2016년 2월 23일
답변: Amos 2016년 2월 23일
I have a file1 with 162000 lines and i want to replace some of them. I also have another file2 with 1600 lines that i will use to replace the lines of file1. I Know for example that first line of file2 will replace 105586 line of file1, second line will replace 104759 line, etc... I also have a third file3 with these numbers (105586,104759,...). The code i tried to use is:
N_lines=162000
M_lines=1600
fin=fopen('/my_path/file3','r');
for i=N_lines;
data=fgets(fin);
for j=M_lines;
file1(data,:)=file2(j,:);
end;
end;
However, this error occurs: Subscripted assignment dimension mismatch.
Can anyone help me with this problem?
Thank you in advance!
  댓글 수: 2
Amos
Amos 2016년 2월 23일
What is the data in your files? Is it numbers? If it is a file containing numbers in a regular pattern, say with m rows and n columns, then one could also use commands like "dlmread".
Kona
Kona 2016년 2월 23일
Thank you for your answer.All data in files are numbers and files are .csv.

댓글을 달려면 로그인하십시오.

채택된 답변

Amos
Amos 2016년 2월 23일
Maybe something like:
line_numbers = dlmread('file3');
data1 = dlmread('file1');
data2 = dlmread('file2');
for i=1:length(line_numbers)
data1(line_numbers(i),:) = data2(i,:);
end
dlmwrite('file1_modified',data1);
Note, this is just dummy code. You will need to adapt it to care for headers (if any) in your data files and the specific data format.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by