Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

textscan not working with double quotation marks

Asked by DoVile Last Name: on 28 Dec 2012

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 :)

0 Comments

DoVile Last Name:

Products

1 Answer

Answer by Walter Roberson on 28 Dec 2012
Accepted answer

"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 Comments

DoVile Last Name: on 28 Dec 2012

Well i run

PWT = 'Data ind\PWT6.txt';
fid=fopen(PWT,'rt');  
tmp = textscan(fid,'%f%q%f%f%f%f%f%f');                          
celldisp(tmp)

And it then returns only the first line of the document, for some reason i dont think it recognizes the line change meaby ?

Walter Roberson on 28 Dec 2012

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: on 29 Dec 2012

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 :)

Walter Roberson

Contact us