Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Variable length tab deliminated txt files

4 views
Skip to first unread message

Garry Higgins

unread,
Dec 17, 2009, 12:08:05 PM12/17/09
to
Hi guys,

I'm working with some data in txt files and need some help with my textscan command.

Initially the files were all the same length and each result was tab deliminated with a new line character denoting a new set of data. It's of the form:

2.343\t2.453\t6.332\t34.623\n
3.532\t3.235\t8.902\t53.643\n

I was parsing this with the command:
nums = textscan(fid, '%n %n %n %n', 'delimiter',char(9));
and all was working well. I'd get a cell out with each entry being a different data set.

Now I need to do the same thing but the number of data entries will vary. So I'll have:

2.343\t2.453\t6.332\t....x....\t34.623\n

Is there an easy way to do this without having to specify the number of floats (%n %n %n.....) in the textscan command? It seems like it should be possible but I've spent the day at it and don't seem to be any closer. Maybe it's too close to Christmas for my brain to work properly :-)

Garry Higgins

unread,
Dec 17, 2009, 12:31:05 PM12/17/09
to
"Garry Higgins" <mathworks...@xoxy.net> wrote in message <hgdohl$d4n$1...@fred.mathworks.com>...

I've just solved my own problem :-)
It might not be the most graceful solution but I'll post it here in case it's of use to someone else.

I started by reading the whole file into a cell using the newline character as deliminator:
tmpChar = textscan(fid, '%s', 'delimiter', '\n');

Then looped through it and returned the results I wanted to a new cell:
for i = 1:length(tmpChar{1})
tmp = tmpChar{1,1}(i);
resultsCell = cell2mat(textscan(tmp{1}, '%n');
end

There's some blank lines in the txt files that are causing it to fail so if someone has a nice solution to this I'd love to hear it? Otherwise I'll just cut them out of the original files as they aren't important.

0 new messages