fopen 'w' not writing

조회 수: 4 (최근 30일)
David Phung
David Phung 2014년 12월 5일
답변: David Phung 2014년 12월 6일
The following is my code. I seem to not be able to write the code into the phone.txt file. can anyone tell me where my error is? phonenos.csv is an file with 3 lines of 10 digits, reminiscent of a phone number without the dashes or parentheses. this code will write them into the format (xxx) xxx-xxxx
format long g
fid=fopen('phonenos.csv','r');
fid2=fopen('phone.txt','w');
for i=1:3
phonenos=fgetl(fid);
area=phonenos(1:3);
next3=phonenos(4:6);
last4=phonenos(6:9);
phonenos=['(' area ') ' next3 '-' last4];
end
close=fclose(fid);
close2=fclose(fid2);
type phone.txt

채택된 답변

Orion
Orion 2014년 12월 5일
You forget a fprintf(fid2,... line, so you are not writing anything with this code, just open and close the phone.txt file.
you need to complete this
fid=fopen('phonenos.csv','r');
fid2=fopen('phone.txt','w');
for i=1:3
phonenos=fgetl(fid);
area=phonenos(1:3);
next3=phonenos(4:6);
last4=phonenos(6:9);
phonenos=['(' area ') ' next3 '-' last4];
fprintf(fid2,................) % line to complete
end
close=fclose(fid);
close2=fclose(fid2);
  댓글 수: 1
Image Analyst
Image Analyst 2014년 12월 5일
I'd use the t option because they're text files.
fid = fopen('phonenos.csv', 'rt');
fid2 = fopen('phone.txt', 'wt');
From the help for fopen:
To open files in text mode, attach the letter 't' to the permission argument, such as 'rt' or 'wt+'.

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

추가 답변 (1개)

David Phung
David Phung 2014년 12월 6일
Thank you, it works like a charm!

카테고리

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