Problem in defining a column
1 次查看(过去 30 天)
显示 更早的评论
Hello,
%I want to define the column 4 of a set of txt imported data. Thus: % I import the data like:
fid = fopen('ex.txt');
data = textscan(fid,'%s%s%f%f%f%[^\n]','headerlines',1);
% (mo delimiter specification, since the file consists of mixture of delimiters)
%Read it as text:
frewind(fid);
txt = textscan(fid,'%s','\n');
fclose(fid);
%Define Column 4 (Price):
Price = data{4};
% In the latest however I receive a value '[]', instead of producing a '10000000x1 double' since 10000000 are the rows of the file?
Can anyone help on what '[]' pertains to and how can overcome this?
Many thanks in advance,
Panos
0 个评论
回答(2 个)
Walter Roberson
2011-4-5
You should be using
fid = fopen('ex.txt', 'rt');
but it is unlikely that that would cause the problem you see.
Please examine your file outside of Matlab and verify that it has spaces between the strings (e.g., not tabs)
3 个评论
Walter Roberson
2011-4-6
After the textscan(), display size(data) and
cellfun(@size,data,'Uniform',0)
It is possible for data{4} to be empty if, for example, data{1} had captured the entire line.
Matt Tearle
2011-4-6
OK, this is the follow-on to this earlier question, right? So an output of [] from textscan generally indicates a mismatch between the format specifier and what's actually in the file. If your data is too big for the memory you have available, you should have seen an "out of memory" error. If not, it's more likely that something doesn't match. Are you sure the file has exactly the same formatting as the example? What about missing values? Extra header lines?
Try reading a fixed number of lines:
data = textscan(fid,'%s%s%f%f%f%[^\n]',10,'headerlines',1);
will read 10 lines. See if you get anything.
Also, do you get anything in txt? You might be able to look at its contents to see what's going on.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Text Files 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!