Importdata won't work with this txt file

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

1 commentaire

Jason
Jason le 20 Juin 2011
I've just checked and my program that creates these txt files was set with the txf file encoding as "Unicode". When I switch it to ANSI, import data and textscan work. The problem is that I have all my data in this UNICODE format. Can importdata or textscan work with this?

Connectez-vous pour commenter.

Réponses (1)

Gerd
Gerd le 20 Juin 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 commentaires

Gerd
Gerd le 20 Juin 2011
You could also use fgetl(fid) to examine the data row by row
Jason
Jason le 20 Juin 2011
Hu, Im sorry but I still can't get it working with textscan. When I use your second suggestion fgetl, then the line that is read in has for some reason put an extra space between the characters!
>> tmp=fgetl(fid)
tmp =
F i l e : C :
Gerd
Gerd le 20 Juin 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
Jason le 20 Juin 2011
hi Gerd, I would like to be able to pull out a single matrix and thats why I thought textscan would do thr trick. Whilst fgetl works, it doesnt feel as elegant as I need to work out the number of times to perform fgetl, and then execute this that many times.
Jason
Jason le 20 Juin 2011
I've also noticed when I use the fgetl(fid) way, there are strange symbols at the very begining that are't present when I open in notepad. Could this be the cause of why importdata and textscan don't work?
>> tmp=fgetl(fid)
tmp =
ÿþ 1 6 . 1 8 4 E - 0 0 1 6 . 1 8 4 E - 0 0 1 6 . 1 7 0 E - 0 0 1
>> fclose(fid)
Gerd
Gerd le 20 Juin 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
Jason le 20 Juin 2011
Could I email it to you? as i don't see any upload facility here.
Gerd
Gerd le 20 Juin 2011
Sure you can do but I will be out of office until tomorrow.
bwts.tre at googlemail dot com

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Import and Analysis dans Centre d'aide et File Exchange

Question posée :

le 20 Juin 2011

Community Treasure Hunt

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

Start Hunting!

Translated by