필터 지우기
필터 지우기

Problem with reading text file and making changes

조회 수: 1 (최근 30일)
GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 1월 2일
편집: Guillaume 2018년 1월 3일
I have a file in a txt format with the following data
0 584547.75 4052042.76
1 584543.25 4052030.13
2 584542.06 4052009.10
3 584556.55 4052005.46
4 584565.18 4052000.66
5 584576.33 4051992.03
6 584594.05 4051981.91
7 584599.05 4051986.77
8 584609.27 4052003.61
9 584604.24 4052007.05
10 584603.98 4052020.41
11 584602.32 4052021.53
12 584605.89 4052029.20
13 584601.43 4052036.84
14 584597.00 4052041.34
15 584584.83 4052048.62
16 584576.23 4052056.49
17 584569.15 4052051.59
18 584555.92 4052047.8
I want to know through which commands, it can be openned, so as to abolish the first row and so as for a comma to be put between the second and the third column. I started with the following code:
fileID = fopen(C:\Users\GB\Desktop\topo);
c = textscan(fileID,'%f %f %f')

채택된 답변

GEORGIOS BEKAS
GEORGIOS BEKAS 2018년 1월 3일
I have found the answer:
M = dlmread('C:\Users\G\Desktop\topo.txt',' '); %replace path as appropriate
M(:,1) = []
dlmwrite('C:\Users\G\Desktop\myfile.txt',M,',')
  댓글 수: 1
Guillaume
Guillaume 2018년 1월 3일
편집: Guillaume 2018년 1월 3일
"I have found the answer"
... to a different question than you asked. The above will put a comma between all the columns not just "between the second and the third column" as you asked.

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

추가 답변 (1개)

Guillaume
Guillaume 2018년 1월 2일
If I understood correctly:
filecontent = fileread('C:\somewhere\somefile.txt'); %replace path as appropriate
filecontent = regexprep(filecontent, '.*[\n\r]+', '', 'once', 'dotexceptnewline'); %remove first line
filecontent = regexprep(filecontent, '(\s+)([0-9.]+\r?)$', ',$2', 'lineanchors'); %replace whitespaces by , before last column
fid = fopen('C:\somewhere\newfile.txt', 'w'); %replace path as appropriate
assert(fid > 0, 'failed to open file for writing');
fwrite(fid, filecontent);
fclose(fid);

카테고리

Help CenterFile Exchange에서 Data Import and Export에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by