Problem in defining a column

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

Answers (2)

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 Comments

Thanks Walter,
It has a mixture of delimiters (spaces and tabs), that's why I didn't define delimiter in textscan.
Should I do this for both?
However when I run this to a sample of the file it works fine.
What '[]' means?
[] is an empty matrix
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.

Sign in to comment.

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.

6 Comments

Thanks Matt,
Yes it's a follow up on this code.
The original file has a mixture of delimiters ('tab' and 'space'). How can I apply this to the textscan code to include more then one delimiters?
By now, and for this reason, I run 'textscan' withoutout any delimiter specification. Could this be the problem?
Panos
You can use 'Delimiter', ' \t' I believe. That is, I seem to recall that \t is recognized in that context.
The default delimiter is "white space" which includes tab and space, so that shouldn't be the problem.
You can put literal characters in your format specification, so you could try something like
data = textscan(fid,'%s%s\t%f%f\t%f%[^\n]',10,'headerlines',1,'delimiter',' ');
to state that there's a tab between columns 2 & 3 and 4 & 5. But, again, I don't think that's the problem here.
Have you tried reading a small fixed number of lines? What do you get inside the cell array (data)?
Can you post a copy of the first few lines of the file?
Thanks Matt,
The result of the data= Textscan is a <1x6 cell> with the following contents:
'????' '01/06/2009' 10281576 1 [] []
The first rows of the file are:
STOCK DATE TIME PRICE VOLUME MARKET
??? 04/01/2010 10293955 18.34 500 ?????? ?????.
??? 04/01/2010 10293955 18.34 70 ?????? ?????.
??? 04/01/2010 10293955 18.34 430 ?????? ?????.
??? 04/01/2010 10293955 18.34 200 ?????? ?????.
??? 04/01/2010 10293955 18.34 100 ?????? ?????.
??? 04/01/2010 10293955 18.34 40 ?????? ?????.
??? 04/01/2010 10293955 18.34 215 ?????? ?????.
First and last column are in Greek. Fourth column is Price with the same value for the below arrays (18.34).
However the copy/paste puts values of price (18.34) near column 3 values (Time) which in the above rows is 10293955. If you see these are seperated by a single 'space'. I left it as it is supposing that this is interesting for the file's format.
In the original file columns 3 abd 4 (Time and Price) are 'tab' seperated.
Is there any way to attach the file (sample with first rows)?
Panos
Hi again Matt,
I just saved the above and greek characters appear as '?'.
The 'tab' delimiters do not appear as well.
Is there any way to attach (send) a sample of the original file?
Many thanks again
Panos
I also get the below error message:
outstr = strcat(txt{1},[' Trade';buysell]);
??? Error using ==> cell.strcat at 50
All the inputs must be the same size or scalars.

Sign in to comment.

Categories

Tags

Asked:

Pap
on 5 Apr 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!