Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations waross on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

reading in mixed data from text file using fgetl and sscanf

Status
Not open for further replies.

JeffEriksen

Bioengineer
Aug 13, 2007
4
I am trying to read in text files with some variability in each line. This is my M-file, with an example file in the comments:

% read in BESA source files

%BSA_1.04.19990715|HC
%type x-loc y-loc z-loc x-ori y-ori z-ori color state size
%=======================================================================================================
%RegSrc +14.303585 -51.693123 -15.073231 0.000000 0.000000 0.000000 255 2 4.50 RotOriT|2|-1|-10000|115000|-0.230011|-0.217721|-0.948539|-0.929508|-0.239599|0.280394|-0.288308|0.94616|-0.147231
%RegSrc +26.841261 +3.132195 +42.905132 0.000000 0.000000 0.000000 16711680 4 4.50
%RegSrc -44.842072 +20.549519 +52.561932 0.000000 0.000000 0.000000 49152 4 4.50 RotOriT|2|-1|-10000|130000|-0.643173|0.405789|-0.649355|0.731313|0.576913|-0.363819|0.226987|-0.708897|-0.667831

fid = fopen('Seastone_Cindy spk11 ga3.bsa','r'); % open file
version = fgetl(fid) % read version line
labels = fgetl(fid) % read label line
separator = fgetl(fid) % read separator line

line = fgetl(fid) % read whole dipole line

dip_type = sscanf(line, '%9c', 1)
dip_loc = sscanf(line, '%*s %f %f %f', 3)
dip_ori = sscanf(line, '%*s %*f %*f %*f %f %f %f', 3)
color = sscanf(line, '%*s %*f %*f %*f %*f %*f %*f %d', 1)
state = sscanf(line, '%*s %*f %*f %*f %*f %*f %*f %*d %d', 1)
size = sscanf(line, '%*s %*f %*f %*f %*f %*f %*f %*d %*d %f', 1)
rot = sscanf(line, '%*s %*f %*f %*f %*f %*f %*f %*d %*d %*f %7c', 1)
%ints = sscanf(line, ' %d', 4)
%floats = sscanf(line, ' %f', 9)

fclose(fid); % close file

(here is the output)

version = BSA_1.04.19990715|HC

labels = type x-loc y-loc z-loc x-ori y-ori z-ori color state size

separator = =======================================================================================================

line = RegSrc +13.853089 -9.083188 +24.741783 0.000000 0.000000 0.000000 255 2 4.50

dip_type = RegSrc

dip_loc =
13.8531
-9.0832
24.7418

dip_ori =
0
0
0

color = 255

state = 2

size = 4.5000

rot = ''

As you can see, I do not know how to proceed after reading in the "state" value. I do not know how to parse the following text of "RotOriT", and also probably will have trouble with the | symbol separating the last sets of integers and floats. Could someone suggest a way to parse this file properly? I have tried textscan, textread, fscanf, but run into problems with all of them.
Thanks.



 
Replies continue below

Recommended for you

I guess the problem is the veritcal bars "|" used as delimiters. If you use the "textscan" function then you can specify the delimiter as one of the options.

M

--
Dr Michael F Platten
 
Thank you. I had tried textscan before but was stymied by something else. I have now used it to read most of data, but I still have the variabiilty problem I alluded to in my titel. The data at the end of the lines is variable in that the part beginning with "RotOri" is optional. I read that you can seek to anywhere in the file with textscan, but cannot figure out how to back up, which I would want to do if I read forward and do not find the string "RotOri".

Can I back up with textscan? I am trying to avoid having to open and close the file multiple times, which si why I thought the fgetl/sscanf would be nice, since fgetl automatically parses lines.

 
Klunky but it should get you thinking about how to manipulate text to numbers on the fly. Not sure what you do with all this in terms of array storage, but you should get the idea..
%==================================================
fid = fopen('Seastone_Cindy spk11 ga3.bsa','r') % open file
version = fgetl(fid) % read version line
labels = fgetl(fid) % read label line
separator = fgetl(fid) % read separator line

line = fgetl(fid) % read whole dipole line

R=strfind(line,'R')
str1=line(R(1)+6:R(2)-1);
a = str2num(str1)
dip_type = line(1:7)
dip_loc = a(1:3)
dip_ori = a(4:6)
color = a(7)
state = a(8)
siz = a(9)
rot = line(R(2):R(2)+6)

if isequal(rot,'RotOriT')
str2=line(R(2)+7:end)
str2=strrep(str2,'|',' ')
b=str2num(str2)
ints=b(1:4)
floats=b(5:end)
end
line = fgetl(fid) % read whole dipole line
R=strfind(line,'R')
str1=line(R(1)+6:end);
a = str2num(str1)
dip_type = line(1:7)
dip_loc = a(1:3)
dip_ori = a(4:6)
color = a(7)
state = a(8)
siz = a(9)

line = fgetl(fid) % read whole dipole line

R=strfind(line,'R')
str1=line(R(1)+6:R(2)-1);
a = str2num(str1)
dip_type = line(1:7)
dip_loc = a(1:3)
dip_ori = a(4:6)
color = a(7)
state = a(8)
siz = a(9)
rot = line(R(2):R(2)+6)

if isequal(rot,'RotOriT')
str2=line(R(2)+7:end)
str2=strrep(str2,'|',' ')
b=str2num(str2)
ints=b(1:4)
floats=b(5:end)
end
fclose(fid); % close file
 
Thank you for the string manipulation examples and your time in putting them together.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor