textscan not working with double quotation marks

조회 수: 3 (최근 30일)
DoVile Last Name:
DoVile Last Name: 2012년 12월 28일
I am using textscan to read a .txt file into matlab but i am having some troubles. The data looks like (first two lines)
1950 "Afghanistan" 8150.368 n/a n/a n/a n/a n/a
1951 "Afghanistan" 8284.473 n/a n/a n/a n/a n/a
and i am using textscan as
PWT = 'Data ind\PWT6.txt';
fid =fopen(PWT);
tmp = textscan(fid,'%f %q %f %f %f %f %f %f','delimiter','\r');
The first column is read ok, the countries are all stored on tmp{1}, but it seems like all the rest is stored in tmp{2} when i actually only want the countries there. Am i using %q wrong or what is going on ?
Thanks :)

채택된 답변

Walter Roberson
Walter Roberson 2012년 12월 28일
"delimiter" is the character used to recognize the end of strings, so when you use \r you are asking the string to go to the end of the line.
What you should do is fopen(PWT, 'rt') for text mode, and not set a delimiter.
  댓글 수: 7
Walter Roberson
Walter Roberson 2012년 12월 28일
You did not add the treatasempty option
tmp = textscan(fid,'%f%q%f%f%f%f%f%f','TreatAsEmpty', 'n/a');
Also remember to fclose(fid) afterwards.
DoVile Last Name:
DoVile Last Name: 2012년 12월 29일
I added the treatasempty option and i also realized that i had stupidly taken the wrong format, there are 10 variables not 8 ! this gave me quite a headache :)
Thanks for all the help Walter, it was immensely helpful.
Thanks :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Text Data Preparation에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by