Importdata won't work with this txt file
Show older comments
Hello, I want to take a txt file that contains headerlines and then a matrix of numbers that represents an image. I cannot get importdata to read the file, I get
??? Error using ==> importdata at 214 Unable to load file. Use TEXTSCAN or FREAD for more complex formats.
I also can't get textscan to work. I have attached the file
//-----------------------------------Begining of file----------
File : C:\
Title:
Date : 17/06/2011
Detector 31, NSCG Surface 1: 1mm sample
Size 1.000 W X 1.000 H Millimeters, Pixels 200 W X 200 H, Total Hits = 158280
1 2 3
1 9.781E-003 1.676E-002 2.619E-002
2 2.224E-002 2.936E-002 3.852E-002
//--------------------END OF File
note, there are tabs at the begining of lines and there are 200 rows and columns of data. I dont need to read in the column or row numbers
Answers (1)
Gerd
on 20 Jun 2011
Hi Jason,
I would use the textscan command to extract the data. By using
fid = fopen('Data.txt')
tmp = textscan(fid,'%s%f%f%f')
fclose(fid)
you will get a cell array of data. Now you can extract the data row by row using
cell2mat(tmp(index))
Now you are able to extract whatever you want out of your data. I cannot test this script in detail but I am pretty sure with some examinations on the data it should work.
If you do have problem please post.
Gerd
8 Comments
Gerd
on 20 Jun 2011
You could also use fgetl(fid) to examine the data row by row
Jason
on 20 Jun 2011
Gerd
on 20 Jun 2011
Jason,
what exately do you want to extract from the file?
If you use
tmp=fgetl(fid)
you get your data line by line.
e.g. if you want to seperate the first row 1 9.781E-003 1.676E-002 2.619E-002
you have to use
tmp_split = textscan(tmp,'%f','delimiter','\t')
Now you have a cell array with 4 double values. The first one is your row which you don't need and the rest is your data.
Jason
on 20 Jun 2011
Jason
on 20 Jun 2011
Gerd
on 20 Jun 2011
I don't see the problem with my file. Maybe it is possible for you to put the file somewhere on the net
Jason
on 20 Jun 2011
Gerd
on 20 Jun 2011
Sure you can do but I will be out of office until tomorrow.
bwts.tre at googlemail dot com
Categories
Find more on Data Import and Analysis 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!