Loading tab delimited .txt file with mix of strings and numeric data

I think I'm going crazy, but for the life of me I can't get this .txt file to load into Matlab. All I want is column 2 but I've tried the usual fopen,textscan,fclose route recommended in other posts. I've also tried functions like load, fileread, and some of the customised functions from other users. Nothing is working! I need some help please!
Sample data attached...
Any advice or solutions welcomed!

Answers (3)

.
Later
Try
fid = fopen( 'Test28_Text_table.txt', 'r', 'ieee-le', 'UCS-2' );
cac = textscan( fid, '%s%f%s%s', 'Delimiter', '\t', 'Headerlines', 1 );
fclose( fid );
>> cac{2}
ans =
3.4000
5.1000
80.6000
19.8000
100.4000
...
I get this warning, but it seems to work
Warning: The encoding 'UTF-16' is not supported.
See the documentation for FOPEN.
Notepad++ recognized the encoding of your file.
>> version
ans =
8.1.0.604 (R2013a)
ii=fopen('file.txt')
s=textscan(ii,'%s')
fclose(ii)
out=regexp( cellstr(s{1}),'\d+(\.)?(\d+)?','match')
out=cellfun(@(x) str2num(x{1}),out(~cellfun(@isempty,out)))

3 Comments

Thanks Azzi,
I've tried your code and while ii is assigned with a value of 3, upon using s=textscan with these parameters, all I get is s = "<1166x1 cell>" of empty fields of the form
{}
{}
{}
{}
{}
...
I think there might be an issue with the type of encoding used to generate the code, which was done on a Windows 8 system?
When using a basic fileread approach:
data = fileread(filename);
...and trying to drag the info out by character position I can see that there are additional characters in front of row 1, column 1 and unnecessary white space, i.e.
ÿþN a m e ...etc
Is there something I should be checking in the file encoding? Or is there something matlab can do to account for this? Is this why textscan produces an array of whitespace?
Regards
Kevin
Also, the variable out = Empty matrix: 0-by-1

Sign in to comment.

You can skip the columns you do not want:
ValuesInCol2 = textread('Test28_Text_table.txt','%*s%f%*[^\n]','headerlines',1)

Asked:

on 18 Feb 2014

Answered:

on 19 Feb 2014

Community Treasure Hunt

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

Start Hunting!