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.
% 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.