Loading tab delimited .txt file with mix of strings and numeric data
Show older comments
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!
1 Comment
Azzi Abdelmalek
on 18 Feb 2014
Nothing is attached
Answers (3)
per isakson
on 18 Feb 2014
Edited: per isakson
on 19 Feb 2014
.
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
on 18 Feb 2014
Edited: Azzi Abdelmalek
on 18 Feb 2014
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)))
Jos (10584)
on 19 Feb 2014
You can skip the columns you do not want:
ValuesInCol2 = textread('Test28_Text_table.txt','%*s%f%*[^\n]','headerlines',1)
Categories
Find more on Text Files in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!