Fri 03 Aug 2012 10:58:48 PM UTC, original submission:
When using textscan to read some data from a text file, if a new line starts with a space and the previous line ends with a space, the first data in that line becomes "NaN".
For example, if the text file is as follows:
- 1
2 3
It has two lines, each line starts with a space. The following command is used for reading the file:
data = textscan(fid,'%f %f')
The first line is always OK. If the first line has no space at its end, the second line is also OK. However if the first line has a space at the end (after the number "1" at the end of line 1), the first column becomes [0 NaN 3]', and the second column is [1 2]'.
I traced the problem to the function strread, line 373-380:
## Check for single delimiter followed/preceded by whitespace
## FIXME: Double strrep on str is enormously expensive of CPU time.
## Can this be eliminated
if (! isempty (delimiter_str))
dlmstr = setdiff (delimiter_str, " ");
rxp_dlmwsp = sprintf ("( [%s]|[%s] )", dlmstr, dlmstr);
str = regexprep (str, rxp_dlmwsp, delimiter_str(1));
endif
My workaround is to run line 381-387 before the above lines. Maybe you'll have a better solution for it.
Thanks,
Xuejin
|