How can I import a .txt file correctly using textscan?

조회 수: 2 (최근 30일)
Mango
Mango 2012년 7월 23일
I am having a horrible time trying to import this data set into matlab:
It's a bunch of agricultural futures information, and so it has text as well as numerical data. I would like to import it so that I can read and utilize the dates and data (would like to have the text, but not as important). Anyway, when I use textscan I get ridiculous results and they're unusable.
for example:
fid = fopen('c_year.txt');
data = textscan(fid,'%s %d %s %d ...and so forth for 188 columns', 'delimiter', ',','headerlines',1);
fclose(fid)
I need it not to import as a struct or cell array, I need to be able to split that up afterwards.
Thanks

답변 (1개)

Geoff
Geoff 2012년 7월 23일
편집: Geoff 2012년 7월 23일
The way to have the most control over a line-based txt file import is to read it one line at a time:
while !feof(fid)
linestr = fgetl(fid);
end
Then use textscan or sscanf on the line after reading it.
doc sscanf
If you prefer, you could use regexp to split out the fields from the comma-separated list.
% Returns a cell array of each field (empty fields handled)
toks = regexp( linestr, '([^,]*),|([^,]*)$', 'tokens' );
  댓글 수: 1
Mango
Mango 2012년 7월 23일
I'm not sure if you took a peek at that file, but it essentially has one header line, the first column is all text, then the rest are mixed and matched, some text, some integers, some decimals, etc. I'm not great with MATLAB, so I'm not 100% sure on how you're saying for me to go about it.
so I would write what?
fid = fopen('c_year.txt'); while !feof(fid) linestr = fget1(fid); end
doc textscan(????)
then it will export to a cell array of each field?
Thanks so much for helping.

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

카테고리

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