How can I import a .txt file correctly using textscan?
2 ビュー (過去 30 日間)
古いコメントを表示
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
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' );
参考
カテゴリ
Help Center および File Exchange で Data Import and Export についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!