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!

回答 (3 件)

per isakson
per isakson 2014 年 2 月 18 日
編集済み: per isakson 2014 年 2 月 19 日

0 投票

.
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)
Azzi Abdelmalek
Azzi Abdelmalek 2014 年 2 月 18 日
編集済み: Azzi Abdelmalek 2014 年 2 月 18 日

0 投票

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 件のコメント

Aeronut
Aeronut 2014 年 2 月 19 日
編集済み: Aeronut 2014 年 2 月 19 日
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
Aeronut
Aeronut 2014 年 2 月 19 日
Also, the variable out = Empty matrix: 0-by-1
per isakson
per isakson 2014 年 2 月 19 日
See my answer.

サインインしてコメントする。

Jos (10584)
Jos (10584) 2014 年 2 月 19 日

0 投票

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

質問済み:

2014 年 2 月 18 日

回答済み:

2014 年 2 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by